0
votes

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
1
just use the 'save as' instruction providing the name and path.pbell
The name is on the clipboard. I want to automate naming this way.Jimmbo

1 Answers

0
votes

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