sending three keystrokes to a non-scriptable application

Hi

I am trying to figure out how to send three basic keyboard commands (“left arrow”, “right arrow” and the “t” key) to a non scriptable application (photo mechanic, by camerabits) -

The reason that I want to do this is that when I am editing pictures, I spend so much time hitting just these three keys (“next, next, next, next, tag, next, next, tag, next, next, tag, previous, un-tag, next, next, next…”) that I’m getting carpal tunnel syndrome. If I could set up three -really- basic scripts that each execute one of the key strokes, I could do this using speech recognition while not breaking my wrist. Any ideas on how to do this?

Apologies in advance for a very basic question, but I’m a photographer, not a programmer. Any help would be appreciated!

many thanks,
-jj

Jacques-Jean Tiziou
http://jjtiziou.net

I believe what you are looking for is:


tell application "System Events"
	tell process "Application Name"
		set frontmost to true
		keystroke left
		keystroke right
		keystroke "t"
	end tell
end tell

replacing “Application Name” with the name of the application. Although I am quite new at this applescripting, I think that this is the basic idea. I’m not sure on what the terms for telling the left and right arrows, but I know that this is how you would keystroke to a non-scriptable application

Sorry if I cannot be of help.

ashanks

Hi. I realised that this left & right arrows can be done using ascii characters

This should work:


keystroke (ASCII character 27) --> This should be a left arrow
keystroke (ASCII character 26) --> This should be a right arrow

in place of the other 2 lines. Let me know if it doesn’t work, but I think it should.

Also, I realised you want to do this to have these voice activated.

Just put the script in your “Speakable Items” folder and name it “t”.

Then use the code with only the “t” keystroke line.

Then make another script, replacing the keystroke line with the keystroke (ASCII character 27) and name/save the script “Previous” and drop it in your Speakable Items folder as well.

And another, replacing the keystroke line with the keystroke (ASCII character 26) and name/save it to “Next” and put it in your speakable items folder too.

Turn Speakable Items on in your prefs, and poof! Activated commands that listen for you to say “t” or “next” or “previous”…

On all these, when saving, save as “Application” and “Run-Only”. That makes it so you don’t have to hit the run button each time :slight_smile:

Thanks ashanks-

No problems with getting the voice activation to register, and it looks like it’s running the script- only problem is that it’s still not actually giving me the result- in fact it doesn’t seem to be doing anything.

any other thoughts?
the very simple code i’m using right now is below- if anyone’s really bored and wants to try this at home, the application can be downloaded from http://www.camerabits.com -

many thanks for your time!
-jj

tell application “System Events”
tell process “Photo Mechanic”
keystroke “t”
end tell
end tell

[/quote]

You forgot the setfrontmost to true line

It should be:


tell application "System Events"
tell process "Photo Mechanic"
set frontmost to true
keystroke "t"
end tell
end tell

Otherwise, it should work fine. That line is important. :slight_smile:

ok, i’ve used that whole bit of code- and now i’m getting an error.

when i trigger the script, a little “tag” application opens up, and then i get an error saying:
“NSReceiverEvaluationScriptError: 4”

what am missing?

thanks again for your time-
take care,
-jj
the current version of the code below, saved as an application w/ run only-

tell application “System Events”
tell process “Photo Mechanic”
set frontmost to true
keystroke “t”
end tell
end tell

The Application “Photo Mechanic” must be open/running when you run the script. Is it?

Okay, I d/l’ed Photo Machanic and figured out the bug. Two actually. I think. If you want the keys pressed to be cmd + t (for toggle), then you don’t just do keystroke “t”. Also, the reason why it was erroring is because the tell process “Photo Mechanic” needs to be tell process “Photo Machanic ™” with a TM. Here…copy and paste this code exactally. It should work:


tell application "System Events"
	tell process "Photo Mechanic™"
		set frontmost to true
		keystroke "t" using {command down}
	end tell
end tell

How about that? Work okay?

Ok, got it working. Thanks!

turns out it was indeed the “TM” that was screwing me up. I’d changed the name of the application file because the TM annoyed me, but it looks like it still keeps it in there in the core of the system.

i didn’t need the command down bit because i was actually looking for just a “t” which is used to “tag” images in the preview mode- but anyways, it works. a bit slower , since it swaps back and forth between the two applications each time you invoke the command, but not bad.

Thanks again for your time!
take care,
-jj

No problem. I’m a nature photographer and programmer too. I’m also a 13-year old kid, so time is no problem for me :stuck_out_tongue: Glad to see you got it working. If you ever need any more help, you can email me at: ashanks04 AT yahoo DOT com (the AT and DOT is used to prevent spam).

You’re welcome,
ashanks

ashanks,

It’s great to encounter a 13 year old kid with such a positive and helpful attitude. Welcome to the BBS and thanks for a breath of fresh air. :slight_smile:

– Rob (an old fart who knows many kids who are too self-centered to help anyone but themselves)

Thank you, Rob. :smiley:

It’s good to see my help is of use. At first, I was afraid of getting chewed out by some guy saying “No, that isn’t how it works.” or something, but I was wrong :slight_smile:

I’m completely new to scripting… just got my first mac, yesterday. Anyway, I want to make certain actions in safari speakable. For instance:


tell application "System Events"
	tell process "Safari"
		set frontmost to true
		keystroke "[" using {command down}
	end tell
end tell

… to go back a page. This works, but I’m curious what other ways there are to do this. Are voice commands to keyboard shortcuts the most efficient way to do this?

I’d also appreciate a verification of my understanding concerning what the line “set frontmost to true” does. I suspect it brings safari forward, but it also seems to make the window stay in front or not let other apps in front, too.

Thanks,
Chris

There are more than one ways to do it. This, for example, will also work:

tell application "Safari" to activate
tell application "System Events"
	click menu item "Back" of menu "History" of menu bar item "History" of menu bar 1 of application process "Safari"
end tell

“set frontmost to true” will bring the said application to front. In scriptable applications, you can use “tell application [“some app”] to activate” to pop it to the front (such as the example I gave above) but if an application is not scriptable, you’ll need a help from System Events. You can also use “set frontmost to ture” in scriptable app. Using the same example, here’s how it looks:

tell application "System Events"
	tell process "Safari"
		set frontmost to true
		click menu item "Back" of menu "History" of menu bar item "History" of menu bar 1
	end tell
end tell

Voice command is efficient for some people, so it really depends on personal preferences.

Thanks! Of course I realized after doing about 20 custom commands that they’ve made an app that links keyboard shortcuts to speakable commands… just use “Define a keyboard command”. A window pops up, asks for the shortcut you want and asks for a name.

Thanks for the alternate method though, that looks like it would be great for commands that don’t have preset shortcuts.

And thanks again for the system events explanation. That makes sense.

I think I need to buy a book to really dig into applescript and get the hang of it.

-Chris