Skip to main content
1-Visitor
January 25, 2017
Question

How to create AHK scripts

  • January 25, 2017
  • 2 replies
  • 6296 views

Hi James62 , we are waiting for you  😃

i want to learn how to create AHK scripts.

1.JPG

    2 replies

    1-Visitor
    January 26, 2017

    hi,

    I'll create a 'hello world' example for you probably next week. It might take me some my time, and this week is just terribly busy.

    It will do the ptc_common_name:d=ptc_common_name:mdl thing mentioned in Jose's blogpost. I'll try to do that one without AHK as well as with AHK.

    There is just one thing i'd like to know. When you define a part do you give it different common name from the model name? Can you give me an example of how you name your part and assembly files?

    cyılmaz1-VisitorAuthor
    1-Visitor
    January 26, 2017

    Thank you 

    if my data is;

    FAN_KASNAK_CAPI                         =500

    MOTOR_KASNAK_CAPI                   =400

    E_MESAFEFSI                                  =1200

    KAYIS SAYISI                                    =4

    common name list;

    FAN_KASNAK_KORUYUCU              =   500_400_1200_4_1_R/L

    MOTOR_KASNAK_KORUYUCU        =  500_400_1200_4_2_R/L

    KAYIS_KASNAK_KORUYUCU           =  500_400_1200_4_3_R/L

    KAYIS_KASNAK_ON_KORUYUCU   =  500_400_1200_4_4_R/L

    common name list will change when my data change ( in excel ) and i'll send to creo

    3.JPG

    1.JPG

    2.JPG

    1-Visitor
    January 29, 2017

    hi, ‌,

    Here is the thing. It's prolly not foolproof, but at least something for starters.

    First of all, you need to install AHK as there is no standalone compiler that'd be downloadable as an executable anymore. You can get AHK from here: http://www.ahkscript.org/


    Once you install AHK the compiler should be located under Start -> AutoHotkey -> Convert .ahk to .exe

    ahk1.PNG

    Next you need to import the following nested mapkey into Creo Parametric:

    !******************* Hello World AHK mapkey example **************************

    mapkey -nestedmapkey @MAPKEY_NAMEFrankenstein mapkey example;\

    mapkey(continued) @MAPKEY_LABELHello World;\

    mapkey(continued) %>exportmdlinf1;%>movemdlinffile1;%>ahkscriptexec1;\

    mapkey(continued) %>movemapkeyfile1;%>loadmapkey1;%>mapkeyfromahk1;

    mapkey >exportmdlinf1 @MAPKEY_LABELExport ProProgram to file;\

    mapkey(continued) ~ Command `ProCmdMmProgram` ;#SHOW DESIGN;\

    mapkey(continued) ~ Select `texttool` `MenuBar` 1 `FileMenu`;~ Close `texttool` `MenuBar`;\

    mapkey(continued) ~ Activate `texttool` `SaveAsPushButton`;\

    mapkey(continued) ~ Update `file_saveas` `Inputname` `model_info.lst`;\

    mapkey(continued) ~ Activate `file_saveas` `OK`;~ FocusIn `texttool` `textPH.TextArea`;\

    mapkey(continued) ~ Activate `texttool` `CloseButton`;#DONE/RETURN;

    mapkey >movemdlinffile1 @MAPKEY_LABELMove the model info file to temp dir;\

    mapkey(continued) @SYSTEMxcopy /s /y model_info.lst.1 \%temp\%\\model_info.lst.1*\ndel \

    mapkey(continued) model_info.lst.*;

    mapkey >ahkscriptexec1 @MAPKEY_LABELExecute the ahk script and wait till done;\

    mapkey(continued) @SYSTEMstart /wait C:\\Users\\\%username\%\\Desktop\\AutoHotkey-Script.exe;

    mapkey >movemapkeyfile1 @MAPKEY_LABELMove the mapkey config file to work dir;\

    mapkey(continued) @SYSTEMxcopy /s /y \%temp\%\\mod_mapkey.pro mod_mapkey.pro*\ndel \

    mapkey(continued) \%temp\%\\mod_mapkey.pro;

    mapkey >loadmapkey1 @MAPKEY_LABELLoad mapkey into config and delete config;\

    mapkey(continued) ~ Select `main_dlg_cur` `appl_casc`;~ Close `main_dlg_cur` `appl_casc`;\

    mapkey(continued) ~ Command `ProCmdRibbonOptionsDlg` ;\

    mapkey(continued) ~ Select `ribbon_options_dialog` `PageSwitcherPageList` 1 `ConfigLayout`;\

    mapkey(continued) ~ Select `ribbon_options_dialog` `ConfigLayout.ImportExportBtn`;\

    mapkey(continued) ~ Close `ribbon_options_dialog` `ConfigLayout.ImportExportBtn`;\

    mapkey(continued) ~ Activate `ribbon_options_dialog` `ConfigLayout.Open`;\

    mapkey(continued) ~ Trail `UI Desktop` `UI Desktop` `DLG_PREVIEW_POST` `file_open`;\

    mapkey(continued) ~ Activate `file_open` `Current Dir`;\

    mapkey(continued) ~ Select `file_open` `Ph_list.Filelist` 1 `mod_mapkey.pro`;\

    mapkey(continued) ~ Trail `UI Desktop` `UI Desktop` `PREVIEW_POPUP_TIMER` \

    mapkey(continued) `file_open:Ph_list.Filelist:<NULL>`;\

    mapkey(continued) ~ Command `ProFileSelPushOpen@context_dlg_open_cmd` ;\

    mapkey(continued) ~ Activate `ribbon_options_dialog` `OkPshBtn`;\

    mapkey(continued) ~ FocusIn `UITools Msg Dialog Future` `no`;\

    mapkey(continued) ~ Activate `UITools Msg Dialog Future` `no`;\

    mapkey(continued) @SYSTEMdel mod_mapkey.pro;

    !*****************************************************************************

    Then put the mapkey icon on your ribbon menu. This step is kind of important as for this is a Hello World tutorial. I just kind of wouldn't know where to put the Hello World phrase otherwise. The thing is when learning new stuff, one should always start with a Hello World example.

    ahk2.PNG

    Now save the following code to AutoHotkey-Script.ahk file on your desktop.

    ;Specify the path to model info file
    ModelInfoFilePath := A_Temp . "\model_info.lst.1"

    ;Initialize empty vars (propably unnecessary)
    commonNameExcel := ""
    contentsOfTheWholeMapkey := ""

    ;Read the general mapkey header file
    FileRead, MapkeyHeader, % A_ScriptDir . "\MapkeyHeader.pro"


    ;Start building up the mapkey
    contentsOfTheWholeMapkey .= MapkeyHeader

    ;Check if the model info file file exists and is readable
    If (!FileExist(ModelInfoFilePath) || ErrorLevel)
    {
        FileDelete, % A_Temp . "\model_info.lst.*"
        ;Display error msg
        MsgBox, 0x2010, Error,
        (LTrim
            Model info file does not exist.
           
            The script will be terminated now.
           
        )
        FileDelete, % A_Temp . "\mod_mapkey.pro"
        ;Remove the trailing slash
        contentsOfTheWholeMapkey := RegExReplace(contentsOfTheWholeMapkey, "\\$", "")
        FileAppend, % contentsOfTheWholeMapkey, % A_Temp . "\mod_mapkey.pro"
        ;MsgBox, % contentsOfTheWholeMapkey ;for debugging
        ExitApp
    }

    ;Read the 3rd line of the model info file
    FileReadLine, modelInfo3rdLine, % A_Temp . "\model_info.lst.1", 3

    ;Check if the 3rd line contains the string "PART"
    If !InStr(modelInfo3rdLine, "PART")
    {
        FileDelete, % A_Temp . "\model_info.lst.*"
        ;Display error msg
        MsgBox, 0x2010, Error,
        (LTrim
            Model is not of a PART type.
           
            The script will be terminated now.
           
        )
        FileDelete, % A_Temp . "\mod_mapkey.pro"
        ;Remove the trailing slash
        contentsOfTheWholeMapkey := RegExReplace(contentsOfTheWholeMapkey, "\\$", "")
        FileAppend, % contentsOfTheWholeMapkey, % A_Temp . "\mod_mapkey.pro"
        ;MsgBox, % contentsOfTheWholeMapkey ;for debugging
        ExitApp
    }

    ;Remove the text before part name
    partNameCreoParametric := RegExReplace(modelInfo3rdLine, "^.*?PART\s", "")

    ;Specify the path to the excel file
    ExcelFilePath := A_ScriptDir . "\stuff.xlsx"

    oExcel := ComObjCreate("Excel.Application") ;Create excel object
    oExcel.Workbooks.Open(ExcelFilePath) ;Open the selected existing file
    oExcel.Visible := false ;Set to false if you don't need excel to be seen
    oExcel.DisplayAlerts := false ;This is set to false to suppress prompts and alert messages

    ;Read through the rows of the excel file
    Loop, 11 ; Data is only filled up to the line 10 in the excel file
    {
        partNameExcel := oExcel.Worksheets(1).Range("B" . A_Index).Value
        If (partNameExcel = partNameCreoParametric)
        {
            commonNameExcel := oExcel.Worksheets(1).Range("C" . A_Index).Value
            break
        }
    }

    /**
    * Explanation:
    * cellVal := oExcel.Worksheets("Sheet1").Range("A1").Value ; get value from "Sheet1" sheet, "A1" cell
    */

    oExcel.Quit() ;Close the excel window
    oExcel := "" ;Release the reference

    FileDelete, % A_Temp . "\model_info.lst.*"

    If (commonNameExcel != "")
    {
        ;Read the specific mapkey file
        FileRead, MapkeyBody, % A_ScriptDir . "\MapkeyFile.pro"
        ;Remove first line, the mapkey header
        MapkeyBody := RegExReplace(MapkeyBody, "(^.*?\R)", "")
        ;Add additional carriage return and new linefeed chars
        contentsOfTheWholeMapkey .= "`r`n"
        ;Replace the text COMMON_NAME_TO_REPLACE in the mapkey body with the commonNameExcel value
        MapkeyBody := StrReplace(MapkeyBody, "COMMON_NAME_TO_REPLACE", commonNameExcel)
        ;Finish building up the mapkey
        contentsOfTheWholeMapkey .= MapkeyBody
    }

    /**
    * Mapkey ">loadmapkey1" performs deletion of the file after this script is done,
    * but delete it anyway just in case the file already exists.
    */
    FileDelete, % A_Temp . "\mod_mapkey.pro"
    FileAppend, % contentsOfTheWholeMapkey, % A_Temp . "\mod_mapkey.pro"
    ;MsgBox, % contentsOfTheWholeMapkey ;for debugging

    ;Force the script to terminate itself
    ExitApp

    Compile the AutoHotkey-Script.ahk file to AutoHotkey-Script.exe file using the compiler mentioned above. The compiled executable has to be placed on your desktop as the mapkey above hints, the path is:

    C:\Users\%USERNAME%\Desktop\AutoHotkey-Script.exe

    You could also run it from the ahk file directly if you specified the path in the above mapkey as follows:

    C:\Users\%USERNAME%\Desktop\AutoHotkey-Script.ahk

    Next, save the following line of code as MapkeyHeader.pro on your desktop, make sure you don't add any newlines after:

    mapkey >mapkeyfromahk1 @MAPKEY_LABELMapkey from AutoHotkey script;\

    Lastly, save the following code as MapkeyFile.pro also on your desktop

    mapkey >commonname1 @MAPKEY_LABELRedefine common name;\

    mapkey(continued) ~ Command `ProCmdModelRename` ;\

    mapkey(continued) ~ Select `rename` `RadioGroup1` 1 `session_only`;\

    mapkey(continued) ~ Input `rename` `CommonNameInput` `COMMON_NAME_TO_REPLACE`;\

    mapkey(continued) ~ Update `rename` `CommonNameInput` `COMMON_NAME_TO_REPLACE`;\

    mapkey(continued) ~ Activate `rename` `OK`;

    That's all there is to it for the setup. You can run it by clicking the Hello World icon on your ribbon menu.

    What does this whole frankenstein beast do?

    It reads an excel file called stuff.xlsx located on desktop

    ahk3.PNG

    It goes through column B of the first sheet and if it finds a value that is the same as the part's name then it copies the text from column C and builds the common name change mapkey.

    It then loads and runs that mapkey as part of the nested mapkey above.

    ahk4.PNG

    Finally, following are the OS script lines used to make the sub-mapkeys

    REM following OS scripts are used in the "-nestedmapkey" mapkey

    xcopy /s /y model_info.lst.1 %temp%\model_info.lst.1*
    del model_info.lst.*

    start /wait C:\Users\%username%\Desktop\AutoHotkey-Script.exe

    xcopy /s /y %temp%\mod_mapkey.pro mod_mapkey.pro*
    del %temp%\mod_mapkey.pro

    1-Visitor
    June 20, 2017

    OP, learn basics of java or vb.net