I recently converted my last one mac from os9 to osX. There were few apple scripts with in os9 which were working fine. But after upgrading to OSX, it stopped to work. Actually script was opening the pdf file and copy the contents and pasting them to newly created text file.
I was using event to copy (cmd+c) and paste (cmd+v) data with in text file. Now its giving error, doesn’t understand the event. I was using “sandis Addition” in os 9.
With in script, steps are something like following (its not reading good in osX):
?event ???TyTe? “v” --select text tool
?event ???TyTe? “a” with ?class ?Com? --select all
?event ???TyTe? “c” with ?class ?Com? --copy
My Question is : what could be equivalent to sandis addition in osX (I guess sandis addition is not available for osX) ?
or What else I can use in osX apple scripting, which can do above steps ?
In osx if you have ui scripting enabled, you can use System Events to do keystrokes. Example:
set the clipboard to “hello”
tell application “TextEdit”
launch
activate
make new document at front
end tell
tell application “System Events”
tell process “TextEdit”
keystroke “v” using command down
end tell
end tell
You don’t really need to do all that, but basically that’s how.
You could create a subroutine, then just call the subroutine to copy, paste, etc.
In os 10.3 and above, ‘enable access for assistive devices’ in the Universal Access pane of System Preferences needs to be checked. For os 10.2.8 you need System Events beta. I don’t know where you can find that at apple.com.
I’m guessing that you installed Tiger.
Edited: here’s what I came up with in a google search for “ui scirpting”
Its perfect. I am able to “enable ui” using following as i am on 10.3.0. Now I have no systax error.
Now, I am stuck into next step
It seems that there is not enough time between copy and paste with in script and in my text file, it copies whatever I had on my clipboard previously (not through scripts). Following is complete script which suppose to copy from pdf and then paste into text file and save the text file as current date name. Any help please.
set cd to current date
copy cd to d
set day of d to 1
copy d to f
set month of f to January
set td to (text -2 thru -1 of ("0" & (day of cd)))
set cm to (text -2 thru -1 of ("0" & (1 + (d - f + 1314900) div 2629800)))
set cy to year of cd
set fstr to "bw_" & cy & cm & td & ".pdf"
set thisItem to "Demo:" & fstr as string
tell application "Finder"
if not (exists file thisItem) then
set cy to (text -2 thru -1 of ("" & year of cd))
set fstr to "bw" & cm & td & cy & ".pdf"
set thisItem to "Demo:" & fstr as string
if not (exists file thisItem) then
return
end if
end if
set folderName to the folder of file thisItem as string
set fileName to the name of file thisItem
end tell
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set splitName to the text items of fileName
set nameOnly to item 1 of splitName
set AppleScript's text item
set splitName to the text items of fileName
set nameOnly to item 1 of splitName
set AppleScript's text item delimiters to oldDelims
-- save pdf file as current date in yyyy-mm-dd format
set fcm to month of cd
set fcy to year of cd
set ftd to day of cd
set ffstr to fcy & "-" & fcm & "-" & ftd as string
set finishedFile to folderName & ffstr as string --build the path to the new text file
tell application "Acrobat 5.0"
activate --bring Acrobat Reader to the front so the keyboard commands work
open alias thisItem --open the file we are on
end tell
tell application "System Events"
tell process "Acrobat 5.0"
keystroke "v" using command down --select text tool
keystroke "a" using command down --select all
keystroke "c" using command down --copy
end tell
end tell
tell application "Finder"
activate --in my experience you have to activate the finder to work with the clipboard
set extractedText to the clipboard as string --this is what we copied
set newFile to open for access file finishedFile with write permission --create a new text file based on the file name & location of the PDF we are on
repeat until exists alias finishedFile --sometimes the file doesn't appear fast enough - repeat until it exists
--
end repeat
set eof file finishedFile to 0 --once it does, set the end of the file to 0 (might not be necessary)
write extractedText to newFile --write the text we copied from the PDF to the text file
close access newFile --close access to the file so it can be worked with
end tell
tell application "System Events"
tell process "Acrobat 5.0"
activate --bring Acrobat to the front if not already
keystroke "w" using command down --close the window
end tell
end tell
I came up with this example. If you have a hard time fitting it into your script, then write back.
tell application “Acrobat Reader 5.0”
activate
set the clipboard to “”
end tell
tell application “System Events”
tell process “Acrobat Reader”
keystroke “a” with command down
keystroke “c” with command down
end tell
end tell
tell application “Acrobat Reader 5.0”
set t to “”
repeat until t is not “”
set t to the clipboard
end repeat
end tell
beep 3
return t
I used Acrobat Reader instead, but it should be the same. Actually, copying took a long time for me.