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
Related
Is it possible to get the text of Pages app, from its current cursor position?
My requirement is like, when user type something in "Pages", I have to show suggestions for the word they are typing.
so I want to find out the current or last word, near current cursor position from "Pages" app.
Either by using AppleScript or Accessibility?
Text is not selected.
I am not looking for "Services" also.
For apps other than "Pages", I used Accessibility and appleScript. but for pages I am not finding any way.
I have also tried below AppleScript, but some reason it works perfectly in "Script Editor", but when I use it in my code, it goes to infinite loop.
tell application "Pages"
activate
end tell
tell application "System Events"
tell application "System Events"
key code 123 using {shift down, command down} -- shift-command-left
end tell
tell process "Pages"
keystroke "c" using {command down}
delay 1
tell application "System Events"
key code 124 -- shift-command-left
end tell
set myData to (the clipboard) as text
return myData
end tell
end tell
If I run this AppleScript in my app, it freeze my Mac only, I have to force quit the Mac to stop it.
This works for me using the latest versions of macOS Mojave and Pages
property theApp : "Pages" -- change value to name of any other application (TextEdit)
tell application theApp to activate
delay 3
tell application "System Events"
tell application process theApp
-- Move the insertion point to the beginning of the previous word.
key code 123 using {option down} -- left arrow key while holding option down
delay 0.2
-- Move the insertion point to the end of the next word. (selects the word)
key code 124 using {shift down, option down} -- right arrow key while holding option and shift down
delay 0.2
keystroke "c" using {command down} -- copies selected wprd
delay 0.2
-- Next 2 key code commands attempt to restore cursor location
key code 124 using {option down} -- right arrow key while holding option down
delay 0.2
key code 123 using {option down} -- left arrow key while holding option down
tell current application to set myData to (the clipboard) as text
delay 4
return myData
end tell
end tell
I am unsuccessfully trying to use AppleScript to automate the process of making small edits to a bunch of files. More specifically, I want a script that will:
Open a specific file in QuickTime
Split it into segments of a specified length
Save each segment as an individual file in the same format and with the same quality as the original.
Close the document
Most importantly, I want the script to essentially work unassisted/unmanned.
Here's some more info on what I'm trying to do: http://hints.macworld.com/article.php?story=20100305070247890.
Another user on StackOverflow asked a similar question a while back, but the suggestion does not work.
From the few online discussions I've been able to find, it appears that Apple took away some of the functionality of QuickTime after version 7. I'm currently using 10.3+
Here's another discussion that describes almost exactly what I'm trying to do. As "kryten2" points out, export no longer seems to work in the new version of QuickTime. And, just like "VideoBeagle", I get permissions errors when I try to call the save method.
The code posted by VideoBeagle on that page does not work for me. Here's a modified version:
tell application "QuickTime Player"
open basefile --argument passed to script when executed
set the clipboard to "outputfile"
delay (0.25)
tell document 1
trim from 0 to 60
tell application "System Events"
tell process "QuickTime Player"
keystroke "s" using command down
keystroke "v" using command down
delay 1
keystroke return
delay 3
#click menu item "Save..." of menu "File" of menu bar 1
end tell
end tell
close saving no
end tell
end tell
The code above DOES open the file in QuickTime and trims the file to the correct length, but then it creates an unsaved copy of the file in a new window, closes the original, but does not save the new document. When I experiment with the delay and remove the "trim" function, it will show the Save dialog but won't actually save a file.
Has anyone successfully managed to use AppleScript and QuickTime to save files? ...recently?
Thank you so much!
The best should be to use export function of QuickTime Player 7 if you have the QuickTime Pro authorization (not free, but very cheap). to do so, you also need to download this old QT version from Apple site. it is still available, but Apple promotes the QuickTime Player 7 with basically only read functions.
Still if you want to stick to QuickTime Player (after version 7), there are known issues in scripting while saving. The workaround is to simulate part of GUI as you already start doing.
The script bellow asks for movie file to be processed, defines the new path and name for the modified video, trim from second 2 to second 6 and then use the GUI interface to save and close. I made many comments to make sure you can understand and update for your own needs :
-- select file to be processed
set myVideo to choose file with prompt "Select video to be processed"
-- set new path and file name
set newPath to ((path to desktop folder from user domain) as string) & "Test_Folder"
set PPath to POSIX path of newPath
set newName to "cutVideo.mov"
tell application "QuickTime Player"
activate
open myVideo
set myDoc to front document
trim myDoc from 2 to 6 -- keep only video from second 2 to 6
end tell
tell application "System Events"
tell process "QuickTime Player"
keystroke "s" using {command down}
delay 0.8
keystroke "G" using {command down} -- go to
delay 0.8
keystroke PPath -- folder path with / and not :
delay 0.8
keystroke return
delay 0.8
keystroke newName -- fill file name
delay 0.8
keystroke return -- ok save dialog
delay 0.8
keystroke "w" using {command down} -- close window
end tell
end tell
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)
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
Im using system events to control a program that does not have a applescript library.
I am therefor using system events to control it.
I have gotten the program to open a pop up window for it Open File interface and I would like to get it to default to a certain location. Is this possible.
So Far I have :
tell application "App Name"
activate
end tell
tell application "System Events"
tell process "App Name"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
tell menu item "Import"
tell menu "Import"
click menu item "XML..."
delay 4
end tell
end tell
end tell
end tell
end tell
end tell
end tell
The pop up window defaults to its own last visited location. I would like it to default to a given file path like /Users/userabc/Documents/abcd.XML
Thanks
If you have the "posix path" of a location and the dialog box open, you can do the following. Note that the location can be a folder or a file path. If it's a file path then that file will be selected and you would then just have to "keystroke return" to close the dialog box and open that file. Good luck.
set theLocation to path to home folder
set posixLocation to POSIX path of theLocation
tell application "System Events"
keystroke "g" using {command down, shift down}
delay 0.5
keystroke posixLocation
delay 0.5
keystroke return
end tell
The only problem with this method is that autocorrect starts filling in as apple script types into the text box and screws everything up. Work around is to copy/paste into from applescript.
The keystroke command doesn't work for inserting characters that can't be inserted with the current input source. And it doesn't work at all with some input sources.
You could also set the value of the text field:
tell application "System Events" to tell (process 1 where frontmost is true)
keystroke "g" using {shift down, command down}
tell window 1
tell sheet 1
set value of text field 1 to "/usr/share/dict/connectives"
click button 1
end tell
click button "Open"
end tell
end tell