On Nov 22, 2015, at 12:10 PM, Yvan KOENIG < email@hidden> wrote:
As you failed to tell it which process must be targeted, System Events send the keystroke to the frontmost one which appear to be the Finder.
Use : tell application “System Events” to tell process "theProcessSupposedToTreatTheKeystroke" set frontmost to true keystroke “f” using {option down} end
The System Events 'keystroke' command always goes to the active application, not to an explicitly targeted process. So, when you want to send a keystroke that acts as a command to an application, such as Command-F (the find command), you do not need to use the double-tell construct that is otherwise required for GUI Scripting. In other words, the double-tell in your example serves only to give the 'set frontmost' command a target. It does not otherwise affect the 'keystroke' command. Your example would work to send a 'keystroke' command because it does bring the target aplication to the front. But you could more clearly follow what it is doing from this example:
activate application "Finder" tell application "System Events" keystroke "f" using {command down} end tell
However, that isn't what the Original Poster wants. He doesn't want to send a command, but instead to type an italics "f" character at the end of a folder name that is currently selected for editing in the Finder, yielding something like "MyFolderName ƒ" as the folder's new name. The OP doesn't explain why he wants to emulate live typing in a script. If he really just wants to change the name of the folder, it would be simpler and more direct to set the name of the folder using simple AppleScript, something like this:
tell application "Finder" set folderList to selection set thisFolder to item 1 of folderList set folderName to name of thisFolder set fChar to "ƒ" -- italics "f" set name of thisFolder to folderName & space & fChar end tell
Or what Thomas Fischer just posted as I was writing this. If the OP is doing this on an older system, he would have to use more convoluted techniques to get the unicode character italic-f.
If the OP really needs to do this by emulating live typing, he has to run the script in a way that keeps the Finder in the foreground, such as by running it from the Script menu. Otherwise, the act of launching the script will send Finder momentarily to the background, and that will cause the live selection of the folder's name to go dead. And, once past that hurdle, the script could be simplified, to something like this (leaving out the second branch addressed to Mail):
tell application "Finder" activate set x to selection if length of x ≠ 0 then tell application "System Events" keystroke "f" using {option down} end tell end if end tell
|