topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 9:33 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: IDEA: Standardize File Name With AHK Dialog Boxes  (Read 6161 times)

pboekers

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 2
    • View Profile
    • Donate to Member
IDEA: Standardize File Name With AHK Dialog Boxes
« on: November 24, 2009, 05:26 PM »
OK here it is.  I am trying to standardize file naming on our systems so that different users are generally forced to apply certain filename attributes to the file. I have started on the AHK script, but start running out of intelligence when it comes to parsing strings, and then joining them together in an array that can copy the string to the clipboard, and then auto paste into a filename field.  In envision this to be a hotkey or event triggered script (preferred), that will present a dialogue box to the user to complete the standardized file name.

I have included the script which I have started below the ----.   What should occur is a file name like this example of a dialogue.

Selected Date: 2009-10-25
Reference # 522225
Sequence # 01
Document Origination And Ownership : C Client
Document Type: IN Invoice
Initial Routing Direction: S Sent Out
Etc. 

With the above example I would process this information by. ...
Select the first letter of "Document Origination And Ownership" which would be "C"
Select the first 2 letters from "Document Type" which would be "IN"
Select the first letter of "Initial Routing Direction" which would be "S"

Then I would pull the descriptions from those same variables like..
Select the 3rd to the end "Document Origination And Ownership" which would be "Client"
Select the 4Th letter FPO the end "Document Type" which would be "Invoice"
Select the 3rd to the end "Initial Routing Direction" which would be "Sent Out"

(Unless there is a way to separate these variables components in the drop down list box like you can do in Excel)

Then I would combine this information into a description into the status bar by joining the above data and adding the red characters below.
522225_01_-_(CINS)_Client_Invoice_Sent_Out_2009-10-25      (Some underscores are added spaces unless included in the variable like in "Sent Out")

During the completion of this dialogue box, I think it would be good to see the filename being composed in the status line.  When the dialogue box is completed, there would be a [OK] button to past this information into the file underlying file name field, and then close the dialogue box.  Yippee, then the core of all our company's file names will be consistent and easily searchable.

Code written so far included below. (Which is activated via Ctrl+T)
----------------------------------------------------

^t::
Gui, Add, MonthCal,  x1 y1 w185 h160 , vMyCalendar

Gui, Add, Text, x190 y7 w80 h20 , Reference #
Gui, Add, Edit, x260 y3 w50 vRefNo

Gui, Add, Text, x330 y7 w200 h20 , Sequence #
Gui, Add, Edit, x390 y3 w20  vSeq

Gui, Add, Text, x190 y30 w200 h20 , Document Orgination And Ownership
Gui, Add, DropDownList, x190 y50 w250 h150 vOwnership , V Vendor|P Company|C Client|O Other


Gui, Add, Text, x190 y80 w200 h20 , Document Type
Gui, Add, DropDownList, x190 y100 w250 h150 vDocumentType , IN Invoice|PS Packing Slip|PO Purchase Order|WO Work Order|CK Check|PD Proofing Document|FF Film|DS Drop Ship Document

Gui, Add, Text, x190 y130 w200 h20 , Initial Routing Direction
Gui, Add, DropDownList, x190 y150 w250 h150 vDirection , S Sent Out|R Returned To Company|F File Only

;Compose Document Code, by extracting document coding letters from drop down boxes.

;Extract The Document Type Codes
 
StringMid, vOTrim, vOwnership, 1, 1
StringMid, vTTrim, vDocumentType, 1, 2
StringMid, vDTrim, vDirection, 1, 1

;Extract The File Descriptions
StringTrimLeft, vOStr, vOwnership, 2
StringTrimLeft, vTStr, vDocumentType, 3
StringTrimLeft, vDStr, vDirection, 2

Gui, Show, w477 h500, New GUI Window
Return

GuiClose:
---------------------------------------------------------
End of code above.

That is it for now. I hope to learn a bit about assigning and combining variables.

Thanks a lot,
Phil

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: IDEA: Standardize File Name With AHK Dialog Boxes
« Reply #1 on: November 24, 2009, 07:00 PM »
Consider the following:

Code: AutoIt [Select]
  1. ^t::
  2. {
  3.     Gui, Margin, 0, 0
  4.    
  5.     Gui, Add, MonthCal    , xm+0   ym+0   w185 h160 Border             vMyCalendar   gOnClick  ,
  6.     Gui, Add, Text        , xm+185 ym+5   w70  h20  Right 0x200                                , Reference #:
  7.     Gui, Add, Edit        , xm+260 ym+5   w50  h20                     vRefNo        gOnClick  ,
  8.     Gui, Add, Text        , xm+315 ym+5   w65  h20  Right 0x200 Number                         , Sequence #:
  9.     Gui, Add, Edit        , xm+385 ym+5   w20  h20                     vSeq          gOnClick  ,
  10.     Gui, Add, Text        , xm+190 ym+30  w200 h20  0x200                                      , Document Orgination And Ownership:
  11.     Gui, Add, DropDownList, xm+190 ym+50  w250 h150                    vOwnership    gOnClick  , Vendor||Company|Client|Other
  12.     Gui, Add, Text        , xm+190 ym+75  w200 h20  0x200                                      , Document Type:
  13.     Gui, Add, DropDownList, xm+190 ym+95  w250 h150                    vDocumentType gOnClick  , Invoice||Packing Slip|Purchase Order|Work Order|Check|Proofing Document|Film|Drop Ship Document
  14.     Gui, Add, Text        , xm+190 ym+120 w200 h20  0x200                                      , Initial Routing Direction:
  15.     Gui, Add, DropDownList, xm+190 ym+140 w250 h150                    vDirection    gOnClick  , Sent Out||Returned To Company|File Only
  16.     Gui, Add, Button      , xm+370 ym+165 w70  h20                                   gClipboard, Clipboard
  17.     Gui, Add, StatusBar
  18.     SB_SetText( "Ready.", 1 )
  19.     Gui, Show, AutoSize, Filename Standardiser
  20. }
  21.  
  22.  
  23. OnClick:
  24. {
  25.     Gui, Submit, NoHide ; Get current variable values.
  26.    
  27.     ; Handle "Document Orgination And Ownership" DDL.
  28.     If ( Ownership = "Vendor" )
  29.     {
  30.         myAbbr1 := "V"
  31.     }
  32.     Else If ( Ownership = "Company" )
  33.     {
  34.         myAbbr1 := "P"
  35.     }
  36.     Else If ( Ownership = "Client" )
  37.     {
  38.         myAbbr1 := "C"
  39.     }
  40.     Else If ( Ownership = "Other" )
  41.     {
  42.         myAbbr1 := "O"
  43.     }
  44.    
  45.     ; Handle "Document Type" DDL.
  46.     If ( DocumentType = "Invoice" )
  47.     {
  48.         myAbbr2 := "IN"
  49.     }
  50.     Else If ( DocumentType = "Packing Slip" )
  51.     {
  52.         myAbbr2 := "PS"
  53.     }
  54.     Else If ( DocumentType = "Purchase Order" )
  55.     {
  56.         myAbbr2 := "PO"
  57.     }
  58.     Else If ( DocumentType = "Work Order" )
  59.     {
  60.         myAbbr2 := "WO"
  61.     }
  62.     Else If ( DocumentType = "Check" )
  63.     {
  64.         myAbbr2 := "CK"
  65.     }
  66.     Else If ( DocumentType = "Proofing Document" )
  67.     {
  68.         myAbbr2 := "PD"
  69.     }
  70.     Else If ( DocumentType = "Film" )
  71.     {
  72.         myAbbr2 := "FF"
  73.     }
  74.     Else If ( DocumentType = "Drop Ship Document" )
  75.     {
  76.         myAbbr2 := "DS"
  77.     }
  78.    
  79.     ; Handle "Initial Routing Direction" DDL.
  80.     If ( Direction = "Sent Out" )
  81.     {
  82.         myAbbr3 := "S"
  83.     }
  84.     Else If ( Direction = "Returned To Company" )
  85.     {
  86.         myAbbr3 := "R"
  87.     }
  88.     Else If ( Direction = "File Only" )
  89.     {
  90.         myAbbr3 := "F"
  91.     }
  92.    
  93.     ; Format calendar to chosen datestamp.
  94.     FormatTime, myDate, % MyCalendar, yyyy-MM-dd
  95.    
  96.     ; Replace spaces with underscores if necessary.
  97.     StringReplace, Ownership   , Ownership   , %A_Space%, _, All
  98.     StringReplace, DocumentType, DocumentType, %A_Space%, _, All
  99.     StringReplace, Direction   , Direction   , %A_Space%, _, All
  100.    
  101.     ; Build filename from user input.
  102.     myFilename := RefNo . "_" . Seq . "_-_(" . myAbbr1 . myAbbr2 . myAbbr3 . ")_" . Ownership . "_" . DocumentType . "_" . Direction . "_" . myDate
  103.    
  104.     ; Populate filename field.
  105.     SB_SetText( myFilename, 1 )
  106. }
  107.  
  108.  
  109. Clipboard:
  110. {
  111.     Clipboard := myFilename
  112.     SB_SetText( "Filename copied to clipboard.", 1 )
  113.     Sleep, 1500
  114.     SB_SetText( myFilename, 1 )
  115. }
  116.  
  117.  
  118. GuiClose:
  119. GuiEscape:
  120. {
  121.     Gui, Destroy
  122. }

Personally, I would have used some coding shortcuts, but I wrote this with readability in mind since you mentioned you wanted to learn from it.  The filename is auto-generated as you type or select from the dropdowns.  Press the "Clipboard" button to copy the filename to your clipboard.  Please let me know if you have any questions.
« Last Edit: November 24, 2009, 07:14 PM by skwire »

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,832
    • View Profile
    • Donate to Member
Re: IDEA: Standardize File Name With AHK Dialog Boxes
« Reply #2 on: November 24, 2009, 11:46 PM »
a small 'enhancement' (if i may...)

adding this code

Loop
{
    ifwinactive, Save As
    {
        gosub filename
        winwaitclose, Save As
    }
    ifwinnotexist, Save As
        gui, destroy
    Sleep, 1500

}

FileName:

to the top of the above script means that the gui will popup automagically whenever a 'save as' dialog is open (it will also destroy the gui when the dialog has been closed if the user hasn't already closed it)

should work with any dialog with 'Save As' in the title - tested fine with both Word and Excel

pboekers

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 2
    • View Profile
    • Donate to Member
Re: IDEA: Standardize File Name With AHK Dialog Boxes
« Reply #3 on: November 25, 2009, 03:14 PM »
 :)  Wow it looks like we are on track.  I will work on this a little more and post again in a day or 2.  Thank you very much.


To Target:

I attempted to add this code and make it "auto pop up" but ti did not seem to work under Word for me, som other programs have other titles which I can add in later, such as "Save Drawing" etc. 

Could you please post the code snippit that you appended this to that worked for you so I could see where I am going wroing.  Also, could I not also just look at the prefix "Save" on the active window to trigger this script?


a small 'enhancement' (if i may...)


Loop
{
    ifwinactive, Save As 
; ***(Could this be written with a StringMid to look at the first 4 characters, and if they = Save, then this is triggered. )***
    {
        gosub filename
        winwaitclose, Save As
    }
    ifwinnotexist, Save As
        gui, destroy
    Sleep, 1500
}

FileName:

to the top of the above script means that the gui will popup automagically whenever a 'save as' dialog is open (it will also destroy the gui when the dialog has been closed if the user hasn't already closed it)

should work with any dialog with 'Save As' in the title - tested fine with both Word and Excel

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,832
    • View Profile
    • Donate to Member
Re: IDEA: Standardize File Name With AHK Dialog Boxes
« Reply #4 on: November 25, 2009, 05:20 PM »
as requested, attached is the complete script I was playing with.

As you can see I've just added a loop to the top of Skwires script to check for the existence of file save dialogs...

you should be able to use save in lieue of save as, though I haven't played with this

it also occurs to me that there is no need to destroy the gui each time as hide/show has the same effect.  The bonus with this is that the last use is persistent, ie you can see the last entry (note that this could be coded in anyway)

#singleinstance

Loop
{
    ifwinactive, Save As ;should work with just save, but i haven't tried it - note that you would need to change the other instances as well
    {
        gosub filename
        winwaitclose, Save As
    }
    ifwinnotexist, Save As
        gui, destroy
    Sleep, 1500

}

FileName:
^t::
{
    Gui, Margin, 0, 0
    
    Gui, Add, MonthCal    , xm+0   ym+0   w185 h160 Border             vMyCalendar   gOnClick  ,
    Gui, Add, Text        , xm+185 ym+5   w70  h20  Right 0x200                                , Reference #:
    Gui, Add, Edit        , xm+260 ym+5   w50  h20                     vRefNo        gOnClick  ,
    Gui, Add, Text        , xm+315 ym+5   w65  h20  Right 0x200 Number                         , Sequence #:
    Gui, Add, Edit        , xm+385 ym+5   w20  h20                     vSeq          gOnClick  ,
    Gui, Add, Text        , xm+190 ym+30  w200 h20  0x200                                      , Document Orgination And Ownership:
    Gui, Add, DropDownList, xm+190 ym+50  w250 h150                    vOwnership    gOnClick  , Vendor||Company|Client|Other
    Gui, Add, Text        , xm+190 ym+75  w200 h20  0x200                                      , Document Type:
    Gui, Add, DropDownList, xm+190 ym+95  w250 h150                    vDocumentType gOnClick  , Invoice||Packing Slip|Purchase Order|Work Order|Check|Proofing Document|Film|Drop Ship Document
    Gui, Add, Text        , xm+190 ym+120 w200 h20  0x200                                      , Initial Routing Direction:
    Gui, Add, DropDownList, xm+190 ym+140 w250 h150                    vDirection    gOnClick  , Sent Out||Returned To Company|File Only
    Gui, Add, Button      , xm+370 ym+165 w70  h20                                   gClipboard, Clipboard
    Gui, Add, StatusBar
    SB_SetText( "Ready.", 1 )
    Gui, Show, AutoSize, Filename Standardiser
}
Return


OnClick:
{
    Gui, Submit, NoHide ; Get current variable values.
    
    ; Handle "Document Orgination And Ownership" DDL.
    If ( Ownership = "Vendor" )
    {
        myAbbr1 := "V"
    }
    Else If ( Ownership = "Company" )
    {
        myAbbr1 := "P"
    }
    Else If ( Ownership = "Client" )
    {
        myAbbr1 := "C"
    }
    Else If ( Ownership = "Other" )
    {
        myAbbr1 := "O"
    }
    
    ; Handle "Document Type" DDL.
    If ( DocumentType = "Invoice" )
    {
        myAbbr2 := "IN"
    }
    Else If ( DocumentType = "Packing Slip" )
    {
        myAbbr2 := "PS"
    }
    Else If ( DocumentType = "Purchase Order" )
    {
        myAbbr2 := "PO"
    }
    Else If ( DocumentType = "Work Order" )
    {
        myAbbr2 := "WO"
    }
    Else If ( DocumentType = "Check" )
    {
        myAbbr2 := "CK"
    }
    Else If ( DocumentType = "Proofing Document" )
    {
        myAbbr2 := "PD"
    }
    Else If ( DocumentType = "Film" )
    {
        myAbbr2 := "FF"
    }
    Else If ( DocumentType = "Drop Ship Document" )
    {
        myAbbr2 := "DS"
    }
    
    ; Handle "Initial Routing Direction" DDL.
    If ( Direction = "Sent Out" )
    {
        myAbbr3 := "S"
    }
    Else If ( Direction = "Returned To Company" )
    {
        myAbbr3 := "R"
    }
    Else If ( Direction = "File Only" )
    {
        myAbbr3 := "F"
    }
    
    ; Format calendar to chosen datestamp.
    FormatTime, myDate, % MyCalendar, yyyy-MM-dd
    
    ; Replace spaces with underscores if necessary.
    StringReplace, Ownership   , Ownership   , %A_Space%, _, All
    StringReplace, DocumentType, DocumentType, %A_Space%, _, All
    StringReplace, Direction   , Direction   , %A_Space%, _, All
    
    ; Build filename from user input.
    myFilename := RefNo . "_" . Seq . "_-_(" . myAbbr1 . myAbbr2 . myAbbr3 . ")_" . Ownership . "_" . DocumentType . "_" . Direction . "_" . myDate
    
    ; Populate filename field.
    SB_SetText( myFilename, 1 )
}
Return


Clipboard:
{
    Clipboard := myFilename
    SB_SetText( "Filename copied to clipboard.", 1 )
    Sleep, 1500
    SB_SetText( myFilename, 1 )
}
Return


GuiClose:
GuiEscape:
{
    Gui, Destroy
}
Return

« Last Edit: November 25, 2009, 05:22 PM by Target »