Applescript Change To Folder Save Dialog - applescript

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

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.

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 keystrokes in a Input Source independent way

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"

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 to Save Preview Document

I'm using a third party application to copy an image to the clipboard. I would then like to perform an AppleScript that opens Preview, creates a New Document which will then use the clipboard as the content, then save this to a specified location.
I'm 99% of the way there - just can't get the document to save. I'm on OS X 10.9.5.
Here's my script so far:
set the save_location to the quoted form of "/Users/freddy/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
activate
save front document as "PNG" in POSIX file save_location
end tell
I can't find the correct syntax for saving a Preview document. It will be the only document open at the time.
Try the following - note the comments:
# Do not use `quoted form of` - only needed and useful with `do shell script`
set the save_location to "/Users/jdoe/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
# Wait until the new window appears.
# Trying to save before the window has appeared will fail.
# Note: - This assumes that NO window was initially open.
# - The code should be made more robust to eventually time out.
repeat until (count of windows) > 0
delay 0.3
end repeat
activate
# Save the document; `as "<format>"` doesn't seem to work, but the
# format is *implied* by the filename suffix (extension).
save front document in POSIX file save_location
end tell
This script works to save an image in the clipboard to disk, using Preview in OS 10.11.6 on a 2010 Mac Mini:
--CONDITIONS: Finder's clipboard has an image in it
tell application "Preview"
launch
activate
end tell
delay 2 --tweak the delays to what works
tell application "System Events" --not as elegant as "tell application...to tell process", but clearer, chronologically
keystroke "n" using command down --Preview creates a new window.
delay 1
keystroke "v" using command down --Finder's clipboard image is pasted into Preview window.
end tell
delay 1
tell application "System Events"
set the clipboard to "Joe Blow" --for name of forthcoming Preview file
delay 1
keystroke "w" using command down --Forces prompt of Preview to save image.
delay 1
keystroke "v" using command down --Pastes new filename into Preview's save window.
delay 1
key code 36 --"Return" key saves the new file.
end tell
--RESULT: A new image exists in your default folder, named "Joe Blow.png" (or whatever extension)

Resources