I’m trying to convert several hundred text clippings into text files with AppleScript v1.9 on OS 10.2.8.
I found this thread over at the MacOSXHints Forums, where the third to last message contains a script that will copy the contents of a clipping to its comments field and create a text file from the clipping.
property the_script_text : ""
property type_list : {"clpt"}
property extension_list : {"textClipping"}
on adding folder items to this_folder after receiving these_items
tell application "Finder"
set SiTem to these_items
repeat with i from 1 to number of items in SiTem
set this_item to item i of SiTem as alias
if file type of this_item is in the type_list or name extension is in the extension_list then
tell application "TextWrangler"
open this_item
tell document 1
set the_script_text to contents as Unicode text
close saving no
end tell
my setComm(this_item, the_script_text)
end tell
end if
end repeat
end tell
end adding folder items to
on setComm(this_item, the_script_text)
tell application "Finder"
set this_item to displayed name of this_item
set oldDelims to AppleScript's text item delimiters -- get normal dilm
set AppleScript's text item delimiters to {".textClipping"} --sets new dilm
set this_item_name to text item -1 of this_item as string ---
set AppleScript's text item delimiters to oldDelims -- resets dilm
do shell script "echo " & quoted form of the_script_text & " > ~/" & quoted form of this_item_name & ".txt"
end tell
end setComm
But, I don’t have TextWrangler, and changing it to TextEdit won’t work. I’m guessing this might be because text clippings store their data in the resource fork of the file, and TextEdit doesn’t know to look there.
Can anyone suggest changes to this script that would work with TextEdit? I don’t need to add anything to the comments field of the file (setComm?), just move the data in the clipping to a plain text file.
Try this… you just need to change the first line to reflect the path to the folder containing your sticky notes. I’m using OSX 10.4 though, so it may not work for 10.2.8.
set sticky_folder_path to alias "MacOSX:Users:hmcshane:Desktop:stickies:" -- the path to the folder containing the sticky notes
set myDelay to 0.25
-- create a new TextEdit document to hold the stickies contents
tell application "TextEdit" to make new document
-- get the sticky notes
tell application "Finder"
activate
set theFolderItems to (get items of sticky_folder_path)
end tell
repeat with anItem in theFolderItems
set file_name to name of anItem -- get the file name of the note
tell application "Finder"
activate
open anItem -- open the sticky note
end tell
tell application "System Events"
keystroke "a" using command down -- select all
delay myDelay
keystroke "c" using command down -- copy text
delay myDelay
keystroke "w" using command down -- close the note
delay myDelay
end tell
tell application "TextEdit"
activate
tell application "System Events"
keystroke file_name -- place the file name before the note text, so you know which file the text came from
delay myDelay
keystroke return
delay myDelay
keystroke "v" using command down -- paste the note contents
delay myDelay
keystroke return
delay myDelay
keystroke return
delay myDelay
end tell
end tell
end repeat
This works for me, setting the variable “A” to the contents of the clipping.
tell application "Finder" to (open alias ((path to desktop folder as text) & "Name of Clipping"))
activate application "Finder"
tell application "System Events" to tell process "Finder" to set A to value of text field of scroll area 1 of window 1
I found a different way read the resource fork and parse the confusing data (but it works only with clippings created with Tiger).
set theFile to quoted form of POSIX path of (choose file of type "clpt" without invisibles)
set theData to do shell script "cat " & theFile & "/..namedfork/rsrc"
set {TID, text item delimiters} to {text item delimiters, ((ASCII character 254) & (ASCII character 255))}
set theData to text items of theData
set theData to item 2 of theData
set text item delimiters to ((ASCII character 0) & (ASCII character 0))
set theData to text item 1 of theData
set text item delimiters to TID
display dialog theData
If you don’t mind using the Satimage OSAX, that can read resources. This works with both Jaguar and Tiger:
property type_list : {"clpt" as Unicode text}
property extension_list : {"textClipping" as Unicode text}
property colon : ":" as Unicode text
property txt_extension : "txt" as Unicode text
on adding folder items to this_folder after receiving these_items
set save_destination_path to (path to current user folder as Unicode text)
set astid to AppleScript's text item delimiters
repeat with this_item in these_items
try
set {file type:file_type, name extension:name_extension} to (info for this_item)
if (file_type is in type_list) or (name_extension is in extension_list) then
-- The following two lines use the Satimage OSAX.
set text_resource to beginning of (list resources "TEXT" from this_item)
set the_text to (load resource text_resource type "TEXT" from this_item)
set AppleScript's text item delimiters to colon
set clipping_name to text item -1 of (this_item as Unicode text)
set AppleScript's text item delimiters to name_extension
set text_file_name to (text 1 thru text item -2 of clipping_name) & txt_extension
set fref to (open for access file (save_destination_path & text_file_name) with write permission)
try
set eof fref to 0
write the_text to fref
end try
close access fref
end if
on error msg
display dialog msg buttons {"OK"} default button 1 with icon caution
end try
end repeat
set AppleScript's text item delimiters to astid
end adding folder items to
Thanks. It looks like Satimage OSAX v2.6.9 will run under OS 10.2. I’ll give it a spin.
Unfortunately, the other scripts wouldn’t work on my system. (“using” and “area” were interpreted as identifiers where AppleScript expected an end of line. I guess I either did something wrong, or OS 10.2 and AppleScript 1.9 didn’t support those terms.)
GUI Scripting wasn’t properly introduced until Panther, although it was possible some time after Jaguar came out to download a beta version of System Events that would do it. For the current purpose, the nice thing about GUI scripting is that you don’t need to use a third-party OSAX. On the other hand, the nice thing about using Satimage is that you don’t need to use GUI Scripting.