Application Loses Focus on Script Execution
  • Hey guys,

    I'm trying to make a simple script to help me scroll down from a hot key. The script is as follows:

    tell application "System Events"
    key code 125
    end tell

    It works great when run from the editor but once I run it from FastScripts the following happens. I hit the hotkey, the current application window loses focus (but the application stays frontmost), THEN the script is run (to no affect because there is no active window).

    What am I missing here? This happens in a few of my scripts that involve keypresses on certain windows.
  • Hi there - what application are you in when you try to activate the script? And what keyboard shortcut are you using for the script? I just tested with the script in Safari and it worked as expected for me without deactivating Safari.
  • I can reproduce the deactivation in Chrome, Safari, Applescript Editor, NetNewsWire etc. I am using F6 and have tried using F13 with the same results. It happens in pretty much all my scripts. For example I have a script that opens gmail in Chrome. Now that I watch more closely I can see that the active chrome window is deactivated first then reactivated once the script is called.


    Wait.

    Now that I think about it I might have figured out the root of the problem. My F6 hotkey links to a script that calls the actual page down script. I have it setup this way so I can quickly change what script I have attached to F6.

    Let me try to explain my setup (this will be difficult).

    The folder hierarchy looks like this:

    command.scpt
    regular.scpt
    config.scpt

    >current (folder)
    --->regular.scpt
    --->command.scpt

    >res (folder)

    ---->PageDown
    ------->command.scpt
    ------->regular.scpt

    ---->NetNewsWire Checker
    ------->command.scpt
    ------->regular.scpt

    ---->PlayPause
    ------->command.scpt
    ------->regular.scpt

    ---->Etc.
    ------->command.scpt
    ------->regular.scpt


    The config script (Hotkey: Option F6) presents me with a dialog box that lets me choose a folder from the "res" folder. It then copies the contents of that folder into the 'Current' folder.



    The regular.scpt runs this command (Hotkey: F6):

    tell me to activate
    tell application "Finder" to get folder of (path to me) as Unicode text
    set workingDir to POSIX path of result

    run script workingDir & "current/regular.scpt"



    The command.scpt similarly runs the command (Hotkey: Command F6):

    tell me to activate
    tell application "Finder" to get folder of (path to me) as Unicode text
    set workingDir to POSIX path of result

    run script workingDir & "current/regular.scpt"


    Of course, now I have solved the problem myself haha. I simply had to remove the "Tell me to activate line" which was pulling away the focus from the current application window.


    Anyway, hope you enjoyed hearing my setup :) Took me quite a while to get right. Now I use a slightly modified version that allows me to use two different 'current' folders with my capslockkey mapped to F13 for one of them and my new UBS pedal mapped to F6 to the other.

    Thanks for a great product.
  • Now there is a new problem, I suspect this is why I added the "activate me" line in the first place. The script now only works on every second keypress. I tried mapping to other hotkeys with same result. Any ideas?
  • I think unfortunately there is an issue with the System Events synthesized keystrokes that sometimes causes keyboard shortcuts to be ignored for a short period because of the synthesized keystroke still being "down" when you try to press the shortcut again (in this case F6). If you wait a second or two between strokes do you see that it works every time and not every other time? Unfortunately I'm not sure how to solve this - it may be an unavoidable side effect of the way Apple implements the synthesized keystrokes.
Start a New Discussion

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!