Keystroke Fn (function) key in Applescript - applescript

Is there any way to keystroke the Fn key in apple script? Ideally I would like to be able to "press it" twice in order to launch voice typing.
Thanks in advance!

delay 0.5 -- time to release modifier keys if the script is run with a keyboard shortcut
tell application "System Events"
key code 63 -- fn
key code 63
end tell
See Events.h or my website for the key codes.

In an applescript your file path has to be a macpath not a unixpath i.e., separate the directories with a colon (not a forward slash) e.g.,
tell application "Finder"
open folder "Macintosh HD:Users:username:Library:Speech:Speakable Items"
end tell

Related

APPLESCRIPT: Keycodes double tab and enter not working

hi I have this code to open up a program and find a documentnumber.
When I physically type the keys on my keyboard it works.
I just need to fix the following code, instead off typing, through code creating the keycodes :
tab
tab
enter
So I made this:
tell application "System Events"
key code 48
key code 48
key code 76
end tell
This should work right?
It should work, but it doesn't seem to. Not sure why that is...
Try this instead:
tell application "System Events"
keystroke tab & tab & return
end tell

Applescript: Pasting Clipboard Text into Open/Save Dialog Box

I have many untitled TextEdit files. I'd like to use applescript to save each using, as a name, the text of the top line of each document.
The following will select and copy the first line of a document (not elegant, but it works), but I can't figure out how to paste the clipboard into the save dialog box (and hit "save" afterwards). Can anyone help?
tell application "TextEdit" to activate
tell application "TextEdit"
tell application "System Events" to key code 126 using command down
tell application "System Events" to key code 125 using shift down
tell application "System Events" to key code 8 using command down
end tell
There are 2 ways of doing:
1) the method using GUI scripting: this is what you've started to do. You simulate keyboard events like a user. It is not recommended for mainly 3 reasons: It is usually slow (you need to add delays to leave time for system open window, close them,..). During the script, if user hits key/mouse by mistake, your script will fail. And finally, you're hardly dependent of user interface of the application: if the editor (here Apple with TextEdit) changes something, like a short cut key, your script will no longer work.
Despite that, if you still want to use that way, here is the script that does it for you. I recommend that you add comments as I did (how to remember that key code 8 is 'c' !). I added some extra options to select the path to save (go home folder, enter special path,...). Up to you to use them or not:
tell application "TextEdit"
activate
tell application "System Events"
key code 126 using command down -- command up (cursor at start)
key code 125 using shift down -- shift down (select 1st line)
keystroke "c" using command down -- command C (copy)
keystroke "s" using command down -- open save dialog
delay 0.5 -- to let save as dialog time to open
keystroke "v" using command down -- paste the title from clipboard
-- other options
-- keystroke "h" using {command down, shift down} -- go home directory
delay 0.5
keystroke "g" using {command down, shift down} -- go to dialog
delay 0.5
keystroke "Desktop/Sample" -- path from Documents folder to Sample folder on Desktop
delay 0.5
keystroke return -- close the go to dialog
delay 0.5
keystroke return -- close the save as dialog
end tell
end tell
2) the method using Applescript instructions. It is usually much shorter, more elegant script, much faster to run, and user can't break it during execution. The script bellow does same as script above: It selects the first text row and save the document with that title. Line 1 defines the folder where to save:
set myPath to (path to desktop folder) as string -- path where to save file
tell application "TextEdit"
activate
tell front document
set myTitle to first paragraph
set myTitle to text 1 thru -2 of myTitle -- to remove the return at end of paragraph
save in (myPath & myTitle)
end tell
end tell
I hope it helps

stop applescript script from running on keypress ESC

I have an applescript that sends keystrokes to TextEdit. I need to be able to stop the script at any time from simply pressing the ESC key. I tried some examples online I found but none seem to be working. I have no idea what I am doing lol. Any help would be awesome on this
This is the non working example
activate application "TextEdit"
repeat until (keys pressed) is {"esc"}
set keys_pressed to keys pressed
if keys_pressed is not {} then
tell application "System Events"
key code 18
end tell
delay (random number from 0.2 to 0.3)
tell application "System Events"
key code 19
end tell
delay (random number from 0.2 to 0.3)
end if
end repeat
Im sorry but I don't think you can do this with just AppleScript, but I recommend Cocoa-AppleScript, Download Xcode and make a Cocoa AppleScript App, Cocoa AppleScript allows you to use Cocoa Windows linked to Applescript Code, you can then make a Menubar item to stop the repeating

Applescript Change To Folder Save Dialog

I'm trying to automate opening and saving a file in applescript. I can't seem to get consistent results with the save dialog though. Is it possible to change a save dialog to a specific folder in applescript?
This might help you navigate to a folder once the save dialog is raised:
set the clipboard to "/path/to/your/folder"
tell application "System Events" to tell process "SketchUp" -- I'm guessing on SketchUp name
keystroke "G" using {command down, shift down}
delay 1
keystroke "v" using {command down}
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
You can do it and keep your clipboard intact, I think. If your save dialog is in TextEdit, if you last saved something to the desktop, for example, the following would change your destination back to Documents. It's easier just to use ⌘+D for that, of course, but you can use substitute pretty much whatever path you need. If you have a path with a folder having non-AppleScript allowable characters in the path (such as quotes), you can escape each with the backslash ("\") character.
tell application "TextEdit"
activate
try
tell application "System Events"
keystroke "g" using {shift down, command down}
do shell script "sleep 0.2"
keystroke "~/Documents"
do shell script "sleep 0.2"
keystroke return
end tell
end try
end tell

Is there a way to trigger a Hot Key/Keyboard Shortcut via a Shell Script, a AppleScript or a Automator Workflow?

I'm an avid Keyboard Maestro user and I need a workaround for triggering a keyboard shortcut like ⌘⇧L (externally, without Keyboard Maestro). So I thought a bash script would be capable of doing such a thing. An AppleScript or an Automator workflow would be sufficient, too. I anybody could help me this would be great.
You don't have to read this, but here's why I want to do what I want to do:
I have a the same string assigned to various Markdown macros, I use a string instead of Hotkeys because it's much more memorable for me since my brain already is filled with so many application shortcuts. The disadvantage is that Keyboard Maestro won't delete the keystrokes of the string. I can perform several actions within the program to delete them but adding these actions for each and every macro is tedious and suboptimal.
tell application "System Events" to keystroke "l" using command down & shift down
tell application "System Events"
key code {123, 124} using {shift down, command down} -- ⇧⌘←, ⇧⌘→
keystroke "c" using command down -- keystroke "C" would be treated as ⇧C
end tell
delay 0.02 -- you need a small delay here before the next command
set txt to Unicode text of (the clipboard as record)
Reference of Mac key codes: lri.me/chars

Resources