Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript: Keys Pressed test?

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 )

Posted on Oct 6, 2010 7:40 PM

Reply
Question marked as Best reply

Posted on Oct 6, 2010 8:20 PM

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...
11 replies

Oct 6, 2010 9:23 PM in response to William Donelson

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.

Oct 6, 2010 10:22 PM in response to William Donelson

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

Oct 7, 2010 1:11 AM in response to William Donelson

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.

Oct 8, 2010 2:01 AM in response to taylor.henderson

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

Oct 8, 2010 7:55 AM in response to Andreas 

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.

Oct 8, 2010 8:50 AM in response to William Donelson

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

Oct 9, 2010 7:36 AM in response to Andreas 

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

AppleScript: Keys Pressed test?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.