• Moose
  • Announcements

    • Dirktator

      Please help us for July   06/30/2017

      This month we're hoping for a little bit of a boost this month or next, in addition to covering the regular bills, we could benefit from some hardware upgrades. STEX Collector's Set gifts can now be sent to you via digital download as well as regular mail if you wish! Every donation dollar goes toward keeping Simtropolis online and humming along. Your contribution counts! Hardware Upgrades As our website community software improves with more and more features, and as we bolt on additional features such as chat, the increased load to handle all these new toys is taking the server a bit to task. You may have noticed that we'd been forced to temporarily disable the chat as we worked out allocating resources for more optimal performance. Following this, we've applied a number of 'soft' changes such as caching options and experimented with server configurations (nginx as proxy, for example), so the hardware aspect is also an area we'd like to focus on. The last time we upgraded hardware was back in 2012, prior to the release of SC13. Your contribution means a lot! Your donation will go toward helping us to 1.) migrate to a newer server or 2.) at least get some ram/cpu/storage updates and any other infrastructure hardware or services. Donate and Get a Gift or Donate Any Amount Thanks so much! - Dirktator & The Admins
  • AutoHotkey as a Modding Tool


    nos.17

    For those who are not familiar with it, Autohotkey is a fantastic open source program designed to replicate repetitive keyboard or mouse actions. As with any sort of coding, the possibilities are technically limitless, but so far I have used small script (about 20 lines) to save and open new exemplars in LeProp, and add/remove properties dozens of props in the Reader. What would have taken probably 20-30 minutes, one is able to achieve in under 5 seconds!

    As I said above, the possibilities are limitless, so I will make this tutorial as general as possible while showing examples of scripts I have created.

    This tutorial will be continuously updated as suggestions for inclusions are offered.

     

     

     Introduction 

    This tutorial is aimed towards any custom content creator who regularly, or semi-regularly has to perform repetitive tasks in the Reader or any other application. Simple text scripts can be used to quickly and easily automate such tasks. Skills learned can also be applied to any program or procedure outside of Sc4 also.

    This tutorial is worded simply so those with zero programming experience can complete their first script. This tutorial only covers a few of the basics. There are hundreds of more advanced functions for more experienced users. The AutoHotkey Help file is a great resource for this.

     

     

     Getting Started 

    Download AutoHotkey here. You will notice that there is no interface to start up and code into. This is because everything is done through simple text files.

    The first step is to identify a procedure you would like to automate, for example, adding Prop Family properties for a collection of 80 props. You will need to know exactly what you are doing for the task at hand. As you go through the process manually for the first few times, make careful notes of when and where you click, what you type in, etc. It may be helpful to write a procedure down on paper as you go.

    The second step is to create a new *.txt file that will be used for the script. It is not important at all what it is named or where it is placed. As long as its file extension is changed to *.ahk at some point, it will become associated with Autohotkey and everything will work fine.

     

     

     Sending Key Presses 

    The most basic command is the Send command. Typing

    Send asdf

    will, upon execution of the script, will type out asdf automatically. Not very useful you say? Yes, you’re right.

    Some special keys can be activated by enclosing their name in braces { }. Be careful here. Typing

    Send Enter

    will literally type out Enter as text: that is, the letter “E” followed by “n,” “t,” “e,” “r.” This does NOT have the same effect as pressing the enter button (obviously, typing “Enter” into Word will not put the cursor on a new line – the same concept applies to scripts). To simulate pressing the Enter button,

    Send {Enter}

    should be used instead. Other examples include {Shift}, {Delete}, {Tab}, etc.; for the arrow keys, {Up}, {Down}, {Left}, {Right} are the appropriate commands. Any of the other special keys are called using special characters instead:

    !  .......... Alt
    #.......... Windows

    +.......... Shift
    &
    ......... Used to combine two keys to a single new one (eg. f&g = hold “f” and press “g”)
    ^ ......... Ctrl
    < , > .... If there are a pair of keys, <! Signifies Left Alt and >! Signifies Right Alt; ! would trigger when any Alt is pressed, for example.

    From this, we can refine our script a little more:

    !u::
    Send asdf

    Will type “asdf” when Alt and the 'u' key is pressed. A little more useful, don’t you think? Note the double colon after the statement. This demarks the start of the code that will be executed when those buttons are pressed. Adding a

    return

    demarks the end of the code for that hotkey. For simple scripts, it is not strictly needed, but for good practice, we will amend the code to include it anyways:

    !u::
    Send asdf
    return

     

     

     Sending Mouse Clicks 

    Every page can technically be operated without a mouse, but using 50 Tabs or Shift-Tabs to advance focus to the button you are looking for is inefficient, time-consuming, and dumb. There are numerous ways to do this; by far the simplest method is to use mouse clicks. The syntax

    Click xx, yy

    or, for a right click,

    Click right xx, yy

    simulates a left or right click at xx and yy, where xx and yy are the x and y coordinates of the button on the window you are using. Figuring out these coordinates will probably be the most time-consuming part. Fortunately, there is a tool which comes packaged with AHK that makes this process simple. Navigate to the AHK installation folder and run au3_spy.exe (or type it in the start menu).

    Among other things, the program will give you a live readout of the x and y coordinates of your mouse pointer for the current window and your overall screen. USE THE COORDINATES FOR THE CURRENT WINDOW. The “On Screen” coordinates are marked “less often used,” which is apt. You want to use the mouse position in the active window for numerous reasons, the largest being that if you drag the window around your screen, the Active Window coordinates will remain constant while the Screen coordinates will change.

    vod9RYn.png

     

    This is where careful notes come in handy. Going through the process manually with au3_spy open in the background, recording somewhere the x and y coordinates of any buttons needed in your task. For example, we could write a script that (in notepad) will paste something from the clipboard with minimal effort:

    !u::
    Click right
    Send {Down}{Down}{Down}{Down}{Enter}
    return

    NOTE: In the example above, it is possible to use a mouse click to press the paste button instead of pressing Down 4x and enter, however, this method will be dependent on coordinates of the initial right click. Thus, many times it may be easier to chain arrow key commands instead of locating mouse positions.

     

    To transition to the next topic, suppose we have the following code:

    #s::
    Click right
    Send {Down}{Down}{Down}{Enter}{Enter}
    return

    Which, when run on my internet browser (Chrome) will right click where the cursor is, open the save dialog box, and hit “Enter” to finalize the save. Run it, and you will notice that it does not entirely do what we want it to do. It will open the save dialog box, but the second enter (to finalize the save) does not seem to execute.

    This is a simple error in timing. When the script executes, all of the commands are sent instantaneously. This is the ideal condition; in real life, however, it takes some amount of time to do everything. Even a time of fractions of a millisecond is enough to throw your code off. If you manually right click and hit save, there will indeed be a small delay until the dialog box appears. Syncing everything may seem like a difficult fix, but the code required is quite simple.

     

     

     Opening New Windows 

    AHK sends its commands very quickly (as it is supposed to do). If click on any buttons that activate new windows or dialog boxes, you may run into problems. As mentioned above, the script will execute correctly, but not as intended; rather, it will send some (or all) of the commands in the time it takes for the message box to appear and become active. The solution is to wait until the new window is active and ready for input. Simply use the command

    Winwait, xxxxxx

    where xxxxxx is the full or partial title of the window. It is important to note that this string can either be the full exact title or just the first few characters. For example, if I am editing a file called nos.17_VehiculosFamilias_1c90deg.dat, I could type

    Winwait, nos.17

    Using just “nos.17” is enough for the script to recognize which window it is waiting on.

    If the title is not explicitly stated in the window like it is below,

    71fa2f158f984f94a653ee2e63aa4a84.png

    Windows Task Manager can give you the appropriate name.

    e4a2aa90b6ba42ca87a208c3ed68f4f7.png

    Remember, only the first part of a window name is required. If the name is long or complex, the first few letters should suffice.

     

     

     

     Loops 

    To repeat tasks multiple times, one could either copy and paste the commands the necessary number of times, or use loops. Loops, if you are not familiar with them, will simply repeat the tasks within the braces { } a specified number of times for example,

    Loop, 4
    {
    Send ^v
    Send {Enter}
    }

    would paste whatever is in the clipboard five times on a new line (another description). For obvious reasons, this method is preferred for larger numbers but is also easier to read and debug for small numbers (like 4) too. NOTE: The number in the loop control starts at 0. So if you want something to happen 4 times you will need to enter 3 (0 1 2 3), and so on. The general equation for the number of times you want to loop: n-1. If you’re interested why, (1) and (2); if not, just try and remember that.

     

     

     

     Comments 

    Comments are used for your convenience. Comments are bodies of text that the computer skips over and will not execute - their purpose is to either explain what should happen when some code executes or anything else the programmer wants to say. As such, they are never required but always recommended. In AutoHotkey, everything after a semicolon (;) on the same line will not parse (they will be completely ignored by the computer). An example:

    Click 380,590 ; Click Apply

    Everything after the semicolon ( Click Apply) will be ignored by the computer. It is only for my benefit to remember what each line is supposed to do. Comments in no way are required on every line. I've merely included them for sake of clarity so others can read my code.

     

     

     

     Saving and Running the Script 

    Whenever you are finished. Simply save your notepad document. If you haven’t already, change the extension from *.txt to *.ahk. Windows might give you a warning telling you that changing the extension may make the file unstable – this is generally a good warning but not applicable here and can be ignored. Changing *.txt to *.ahk will in no way change anything in the file – it will simply associate that file as an AutoHotkey script instead of a lowly text file.

    Once the file is renamed, right click on it and you should see some additional options in the menu.

    k68AcBr.png

    • Run Script” will make the script become active. Hit whatever hotkey(s) you assigned, and your script will execute every time you press the key(s). You will see an icon appear in your system tray. Right clicking there will allow you to do a few different things, including closing the script so it will no longer run (Exit).
    • Compile Script” will automatically convert the script to a *.exe file. It can be exited in the same fashion from the system tray.
    • Edit Script” opens the notepad file and allows the script to be edited. Note that just double clicking like normal to open the file actually has the same effect of “Run Script.” This is one of the other results of changing the file extension to *.ahk.

    tDf21xs.png     JAJ4VvS.png

    • Open
    • Help” opens the AutoHotkey help documents which can be a great help in finding the correct syntax.
    • Window Spy” launches au3_spy.exe. See the "Sending Mouse Clicks" subsection for an explanation of its purpose.
    • Reload This Script” recompiles the script. If any changes were made, they will now be active.
    • Edit This Script” opens the notepad file and allows the script to be edited in whichever editor you assigned as default for the *.ahk extension.
    • Suspend Hotkeys” will keep the script active but disable any hotkeys mapped within the script. Useful if the key combination you assigned is actually used in some other program. The H tray icon will be replaced with a similar looking icon, but with an S.
    • Pause Script” prevents any part of the script from running until it is manually resumed. The tray icon will be replaced with a red one.
    • Compile Script” will automatically convert the script to a *.exe file. It can be exited in the same fashion from the system tray.
    • Exit” completely closes and ends the AutoHotkey process.

     

     

     Examples 

    There is simply an immense amount of things one can do with AutoHotkey scripts. However, the things outlined above should be enough to complete most of your scripts for SC4 modding (they’re all I’ve used so far).

    To make your own, identify something repetitive that you do that can be easily scripted. The two examples below detail my method of solving these problems. As with coding, a multitude of approaches exist, and the only incorrect one is the one that does not work (or perhaps takes an inordinate amount of time).

     

    LeProp_SaveClose.ahk
    For those that do not know, LeProp has a peculiar bug where it can only save one action at a time. As such, one needs to close and reopen to edit multiple things on a lot. It can be quite tedious having to save, close, navigate to, and run the LeProp executable. This script is designed to fix that.

    !x::
    Click 380,590           ; Click Apply
    Click 550,120           ; Click Save
    WinWaitActive, LEProp   ; Wait for “Save Successful!” dialogue box
    Send {Enter}            ; Closes “Save Successful!” dialogue box
    WinWaitActive, iLive    ; Waits for main window to be active (“Save Successful!” dialogue box is fully closed)
    Send {Esc}              ; Close LeProp
    run, C:\Program Files (x86)\iLives\LEProp.exe    ; Opens LeProp
    WinWaitActive, Ilive    ; Waits for main window to be active
    Send {Enter}            ; Opens the “Open File” window

    xZhBDkr.gif

     

    Reader_Add_BuildingPropFamily.ahk

    #a::                                      ; Hit Windows-a to activate
    Click right 1200, 600                     ; Right click in the right pane
    Send {Down} {Down} {Down} {Down} {Enter}  ; Select “Add Property”
    WinWaitActive, Property :                 ; Wait for Property editor to appear
    Click 425, 50      ; Click to expand the Name menu
    Send buil          ; Type to scroll list to Building/prop Family
    Click 250, 105     ; Select Building/prop Family
    Click 450, 255     ; Select Add
    Click 40, 635      ; Click in the Values as text field
    Click 40, 635      ; Not sure why I have this twice
    Send 0x5f85e126    ; Your prop family IID goes here
    Click 435, 635     ; Hit first Apply
    Click 190, 700     ; Hit Second Apply
    WinWaitActive, Reader  ; Change to Reader main window name; partial name is acceptable
    Click 1200, 235    ; Click on Building/prop family property
    Send ^c            ; Copies that property to the clipboard
    Click 870, 1000    ; Click to place focus in the left pane
    Send {Down}        ; Move down to select next exemplar
     
    Loop, 257     	   ; Change to number of exemplars starting count at 0: e.g. if last num is 65, use 66
    {
    Click 1200, 600    ; Click to place focus in the right pane
    Send ^v            ; Paste the property
    Click 870, 1000    ; Click to place focus in the left pane
    Send {Down}        ; Move down to select next exemplar
    }                  ; Repeat

    QhegL7v.gif

     

     

     Conclusion 

    3276643.gif  If anyone has questions or needs help on their scripts, I will be happy to help. If I don't start a thread, just shoot me a PM and we can work out your problems together. Happy modding! ...


      Edited by nos.17



    User Feedback


    Cyclone Boom

    Posted

    Thanks for sharing! :thumb:

    Can you confirm this is for the "How to Do It" challenge?

    Share this comment


    Link to comment
    Share on other sites
    matias93

    Posted

    :boggle:o.O:O:rofl:

    That last animation is... is... oh gawd! Do you know how many days could have I saved when adjusting all my industrial lots to IRM standards with this!?

    And not only that, this will be absurdely useful when working on statistical analysis!! You are already improving my uni grades

    nos.17 and CorinaMarie like this

    Share this comment


    Link to comment
    Share on other sites
    nos.17

    Posted

    @Cyclone Boom ... Yes, it is. I've actually had this in progress for awhile, but the challenge provided a convenient motivation to finish it.

    matias93 likes this

    Share this comment


    Link to comment
    Share on other sites
    Cyclone Boom

    Posted

    OK cool.

    From my limited experience of macro programs, they do have a steep learning curve, but are incredibly powerful time-savers for mundane tasks. Great to know you've found a practical use for SC4 modding! :)

    CorinaMarie, 11241036 and matias93 like this

    Share this comment


    Link to comment
    Share on other sites
    CorinaMarie

    Posted

    Whoa! This is wicked cool. :thumb:

    First a possible nitpick. In the 5th line of LeProp_SaveClose.ahk:

    Send {Enter             ; Closes “Save Successful!” dialogue box

    Shouldn't it be?:

    Send {Enter}            ; Closes “Save Successful!” dialogue box

    (Closing brace after Enter)

     

    And now a question before I download the program. Does it have a provision for?:

    • hold click xx,yy
    • drag to xx,yy
    • release

    For instance if I wanted to draw a cropping box in an image editing program.

    Edit: Ok, I believe I found my own answer. It'd be this:

    Click 100,200 down         ;Click and hold left mouse button down at coordinates 100,200
    Click 200,200,0 Relative   ;Move the mouse down and over 200 pixels (to position 300,400)
    Click up                   ;Release the button

     

    nos.17 and matias93 like this

    Share this comment


    Link to comment
    Share on other sites
    nos.17

    Posted

    @CorinaMarie ... Yep. I just added the extra brace. Thanks for that.

    5 hours ago, CorinaMarie said:

    Edit: Ok, I believe I found my own answer. It'd be this:

    Looks good to me. :) 

    matias93 and CorinaMarie like this

    Share this comment


    Link to comment
    Share on other sites


    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an Account  

    Sign up to join our friendly community. It's easy!  :thumb:


    Register a New Account

    Sign In  

    Already have an account? Sign in here.


    Sign In Now