11 Replies Latest reply: Oct 9, 2010 9:39 AM by Andreas 
William Donelson Level 4 Level 4 (2,495 points)
The appleScript below runs fine in OSX 10.4.11, but in OSX 10.5.8, the "Keys Pressed" bit of the "IF" statment is corrupted. How do I get this to work in 10.5.8?

Thanks



Note: Apologies that Apple 21st century forum technologies do not preserve formatting, indentation etc...

on run
tell application "Finder"
activate
make new Finder window
if (keys pressed) contains "Shift" then
set bounds of Finder window 1 to {15, 34, 1520, 585}
else
set bounds of Finder window 1 to {15, 616, 1520, 1210}
end if
set current view of Finder window 1 to column view
end tell
end run

on reopen
run
end reopen

G5 dual 2GHz, 4.5GB RAM, Mac OS X (10.4.11), ( London, UK )
  • taylor.henderson Level 2 Level 2 (450 points)
    I wasn't aware that applescript under tiger could monitor keypresses.

    Within these forums use
    { code }
    ...code here...
    { code }

    without the spaces between "code" and the brackets to maintain formatting like below.


    ...code here...
  • red_menace Level 6 Level 6 (14,750 points)
    Your code doesn't compile on my 10.4.11 machine either. I'm guessing that you had the Jon's Commands osax installed on your Tiger machine, but that hasn't been updated in 8-10 years. I'm not aware of anything that looks for key presses in vanilla AppleScript, and certainly nothing in the Finder. There is a shell utility that gets the modifier keys, if that is what you are after, although it doesn't generate an event - see this MacScripter article.
  • Andreas  Level 4 Level 4 (1,510 points)
    As red_menace said, it sounds as if in Tiger you had installed Jon’s Commands. You could now on your Leopard disk install those same Jon’s Commands. The commands will not compile, but you can use them in the form:

    if («event JonsgKey») = {"Command", "Option"} then ...


    OR

    if («event JonsgKey») contains "Shift" then ...


    You could alternatively consider using “Extra Suites” (find with Google), but I should warn you that the developer, David Lloyd, has not been responding for some time other than to accept money and issue a registration key.


    Andreas
  • William Donelson Level 4 Level 4 (2,495 points)
    Thank you, all. Very interesting and helpful.

    Why do you think AppleScript cannot detect ShiftDowns? Seems strange.
  • taylor.henderson Level 2 Level 2 (450 points)
    William Donelson wrote:
    Thank you, all. Very interesting and helpful.

    Why do you think AppleScript cannot detect ShiftDowns? Seems strange.


    It has to do with what applescript is designed for, mainly specific task you are hoping to automate and not stay open monitoring etc. As for asking why Applescript can't detect ShiftDown you need to define your terms a little better. System Events can't detect ShiftDown, but can trigger keystrokes from within applescript, and there are extensions that can detect shiftdown which could be called from an applescript.
  • William Donelson Level 4 Level 4 (2,495 points)
    taylor.henderson wrote:
    William Donelson wrote:
    Thank you, all. Very interesting and helpful.

    Why do you think AppleScript cannot detect ShiftDowns? Seems strange.


    It has to do with what applescript is designed for, mainly specific task you are hoping to automate and not stay open monitoring etc. As for asking why Applescript can't detect ShiftDown you need to define your terms a little better. System Events can't detect ShiftDown, but can trigger keystrokes from within applescript, and there are extensions that can detect shiftdown which could be called from an applescript.



    In this case, when launched/clicked, the script opens a Finder windows of a certain size at the bottom of the screen. If the ShiftKey is down when launched/clicked, it opens a smaller window at the top of the screen.

    Is there another way to do this without having two AppleScript apps in the dock?

    thanks
  • Andreas  Level 4 Level 4 (1,510 points)
    I gave you two suggestions that would allow you to use a construct such as:


    if (keys pressed) = {“Shift”} then
            do this
        else
            do that
    end if


    As you already have the Jon’s Commands osax on your Tiger disk, just transfer it to the Leopard disk and give it a try.



    Andreas
  • William Donelson Level 4 Level 4 (2,495 points)
    Andreas  wrote:
    I gave you two suggestions that would allow you to use a construct such as:


    if (keys pressed) = {“Shift”} then
            do this
        else
            do that
    end if


    As you already have the Jon’s Commands osax on your Tiger disk, just transfer it to the Leopard disk and give it a try.



    Andreas


    Hmmm... Did not work. Must be something wrong somewhere.
  • Andreas  Level 4 Level 4 (1,510 points)
    What I last posted, and which you copied, is not code that you can use in Leopard. Because Jon's Commands hasn't been updated, you have to use the rather cumbersome code I gave in my earlier post, i.e. using («event JonsgKey») instead of (keys pressed).

    Also, I’m not sure it’s needed, but it might be, after putting Jon’s Commands.osax into /Library/ScriptingAdditions, to reboot your Mac. You can check by asking for File menu > ‘Open Dictionary’ in Script Editor to see that Jon’s Commands is listed. Then test with, say, if («event JonsgKey») = {“Shift”} then say "OK". (You might want to use copy/paste for («event JonsgKey»)).


    Andreas
  • William Donelson Level 4 Level 4 (2,495 points)
    Andreas  wrote:
    What I last posted, and which you copied, is not code that you can use in Leopard. Because Jon's Commands hasn't been updated, you have to use the rather cumbersome code I gave in my earlier post, i.e. using («event JonsgKey») instead of (keys pressed).

    Also, I’m not sure it’s needed, but it might be, after putting Jon’s Commands.osax into /Library/ScriptingAdditions, to reboot your Mac. You can check by asking for File menu > ‘Open Dictionary’ in Script Editor to see that Jon’s Commands is listed. Then test with, say, if («event JonsgKey») = {“Shift”} then say "OK". (You might want to use copy/paste for («event JonsgKey»)).


    Andreas


    Finally got this to work. Not sure what was wrong, as Jon's Commands were shown in dictionary etc
  • Andreas  Level 4 Level 4 (1,510 points)
    Excellent. I’m very pleased.

    ...and thx 4 the


    Andreas