Applescript keystrokes in a Input Source independent way - applescript

I want to write an Applescript that does multiple copy-and-paste between other keystrokes. So this is a sample of what I've done:
tell application "System Events"
set the clipboard to "^"
keystroke "v" using command down
keystroke "a"
set the clipboard to "~"
keystroke "v" using command down
keystroke "a"
end tell
I'm expecting to get ^a~a typed when running the above script with osascript, but I'm getting ~a~a instead. Looks like the keystrokes are executed after all the "set the clipboard" instructions.
How can I have a sequence of copy-and-paste instructions in the same script?
EDIT: the main reason I'm doing this is to be able to auto-type characters in a way that works independently of the Input Source enabled. So I would get the expected sequence of characters with U.S. or with some other Input Source that otherwise would result in âã if we just had the four symbols above typed in sequence. Adding spaces after the accent characters would work for this kind of Input Source, but would give me ^ a~ a with U.S., so that is not the general solution I'm looking for.

Adding a delay solves the issue. In script bellow, it types in TextEdit :
tell application "TextEdit" to activate
tell application "System Events"
set the clipboard to "^"
keystroke "v" using command down
keystroke "a"
delay 0.05
set the clipboard to "~"
keystroke "v" using command down
keystroke "a"
end tell
I assume that you have good reasons, not explained here, to not just do :
tell application "System Events" to keystroke "^ a~ a"

Related

How to return a value from a keyboard shortcut

sorry but I'm quite clueless about programming in general. I have this clipboard manager that allows me to recall x number of items in my clipboard history, for instance, if I hold down Command+Option+Shift+3, it will paste the 3rd most recent clipboard item. Using this, I am trying to return the value through an applescript in BetterTouchTools. I tried this:
tell application "System Events" to set tempclip to keystroke "3" using {option down, shift down, command down}
end tell
return tempclip
However, I am getting a syntax error. (let me remind you I don't know any programming xD). Could someone please help me with correcting this syntax. I'm very desperate :(
Thanks in advance. <3
You can either use a block like so:
tell application "System Events"
set tempclip to keystroke "3" using {option down, shift down, command down}
end tell
or you can use a single-line command, like so:
tell application "System Events" to set tempclip to keystroke "3" using {option down, shift down, command down}
either will work, but what you did in your script was use a single-line command followed by an end tell, as though you were using a block. That's confusing the compiler.

How to set clipboard with value of selected cell in Numbers using AppleScript

I am trying to automate a repeating task between Numbers and Quicken 2017 using AppleScript.
I would like to take the contents of the currently selected cell in Numbers, set the clipboard with that numeric value, and paste that value into the search field in Quicken.
How can one go about doing that with AppleScript?
Example pseudo code to illustrate intent:
tell application "Numbers"
activate
set myCellsValue to value of currently selected cell
set the clipboard to myCellsValue
end tell
tell application "Quicken 2017"
activate
tell application "System Events"
keystroke "f" using {command down}
delay 0.1
keystroke "v" using {command down}
end tell
end tell
You got it 90% correct. I don't use Quicken, but this worked with Microsoft Word:
tell application "Numbers"
activate
tell active sheet to tell application "System Events" to keystroke "c" using command down
end tell
tell application "Quicken 2017"
activate
tell application "System Events"
keystroke "f" using command down
delay 0.1
keystroke "v" using command down
end tell
end tell
I generally like to minimise the amount by which I employ System Events to issue keystrokes or mouse clicks if there’s another way.
Numbers is very much AppleScript-able. I don’t use it personally, but with the help of this site, I’ve pieced together this example script that I wholly admit is untested (I’d appreciate your feedback if you try it out):
tell application "Numbers"
tell the front document to ¬
tell the active sheet to ¬
set CurrentTable to the first table whose class of selection range is range
tell the CurrentTable to get the value of the first cell in the selection range
if the result is not missing value then set the clipboard to the result
end tell
Quicken is also AppleScript-able, but I can’t find a downloadable copy of Quicken’s AppleScript dictionary in order to piece together an equivalent sample of code. However, your Quicken tell block is exactly right for employing System Events to issue a Cmd+V.
However, if you fancy uploading a PDF of the AppleScript dictionary, I can use it to try and draft something more robust.

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

Applescript: insert text with Eñe

I'm trying to set up a text insertion service in Applescript using the following code:
on run
tell application "System Events"
keystroke "PIÑATA"
end tell
end run
When I run the text it inserts "PlaATA" instead of "PIÑATA". What do I need to do for it to properly recognize the 'Ñ'?
The keystroke command of System Events actually mimics the active keyboard. The way you type ñ on your keyboard is how you use the keystroke command to do so. For U.S., it ⌥n followed by n. Something like this should get you started.
tell application "System Events"
keystroke "PI"
keystroke "n" using (option down)
keystroke "n" using (shift down)
keystroke "ATA"
end tell
That's whack, I know, but if you're just talking about ñ's with Spanish, you could write a routine that finds those and breaks up your string into strings between all the ennes and then cycle through that list with your keystroke commands.
Keystroke can not just take higher range unicode (even ascii) characters. You'll have to copy the text to the keyboard and then paste the text as an alternative method.

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

Resources