Hi
I am rookie at this and was looking for a script that archives my clipboard content (text/imgs) to file. Ideally it would save the files with some kind of timestamp or ina dated folder or something. Can anyone help get me started? I see the text to file scripts, images are a bit trickier eh? Also would I need to create an automator task to actively run all the time?
Any helpful tips, links or suggestions would be greatly appreciated as I have never made a script or an automator task before.
But I don’t recommend to use AppleScript for your project, because reading the clipboard can be quite slow (e.g. when you copy big images to the cp). And you will have to use a lot of «if…else if» switches to find out about the content of the clipboard, then save it into the appropriate file format. I guess building a Cocoa app would result in a more reliable solution.
Presumably you want the archives to be readable by other applications rather than simply holding the data for passing back to AppleScript at a later date.
I’ve been fooling with this today. It’s not complete, but it might get you started. It creates an archive folder on the desktop and in it folders for each date the script’s run. The “date” folders contain files with names based on the time and extensions reflecting the kind of data they contain. With text, the preference is for RTF, or failing that Unicode text, or failing that string. It works well with these and with TIFF data ” though for some reason, as Martin warned, it takes a l-o-n-g time to read TIFFs from the clipboard. It also archives ‘picture’ data (obtained when an file icon is copied to the clipboard), but I haven’t been able to get Preview to open the resulting files in Tiger. (It does in Jaguar.) This is possibly due to my ignorance of the format.
on getFolderAndFileNames()
-- Get the current date's year, month number, day, and time.
-- (Uses old-fashioned methods to work on any system.)
set now to (current date)
set {year:y, day:d, time:t} to now
copy now to b
set b's month to January
set m to (b - 2500000 - now) div -2500000
-- Derive a folder name from the date, in ISO format.
tell (y * 10000 + m * 100 + d) as text to set folderName to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
-- Derive a file name from the time, using "." separators instead of ":".
tell (1000000 + t div hours * 10000 + t mod hours div minutes * 100 + t mod minutes) as text to set fileName to text 2 thru 3 & "." & text 4 thru 5 & "." & text 6 thru 7
return {folderName, fileName}
end getFolderAndFileNames
on getClipboard()
set clipData to missing value
set extn to missing value
tell application (path to frontmost application as Unicode text) to set clip to (the clipboard as record)
-- Successively try to grab RTF, Unicode text, string text, TIFF, and PICT.
try
set clipData to clip's «class RTF »
set extn to ".rtf"
on error number errNum
if (errNum is -1728) then
try
set clipData to clip's Unicode text
set extn to ".utxt"
on error number -1728
try
set clipData to clip's string
set extn to ".txt"
on error number -1728
try
set clipData to clip's TIFF picture
set extn to ".tiff"
on error number -1728
try
set clipData to clip's picture
set extn to ".pict"
end try
end try
end try
end try
end if
end try
return {clipData, extn}
end getClipboard
on saveData(folderName, fileName, clipData)
-- Create a folder on the desktop (if necessary) with the name "Saved clipboard contents"
-- and within it (if necessary) a folder named with today's date in ISO format.
set folderPath to (path to desktop as Unicode text) & "Saved clipboard contents:" & folderName & ":"
do shell script ("mkdir -p " & quoted form of POSIX path of folderPath)
-- Create a file within the "date" folder, named with the script's start time, and write the data to it.
set filePath to folderPath & fileName
set fref to (open for access file filePath with write permission)
try
set eof fref to 0
if (fileName ends with ".utxt") then write -257 as short to fref -- UTF16 BOM.
write clipData to fref
end try
close access fref
if (fileName ends with ".tiff") then tell application "Finder" to tell file filePath to set {file type, creator type} to {"TIFF", "prvw"}
end saveData
on archiveClipboard()
set {folderName, fileName} to getFolderAndFileNames()
set {clipData, extn} to getClipboard()
if (clipData is missing value) then
-- Unidentified data.
else
set fileName to fileName & extn
saveData(folderName, fileName, clipData)
end if
beep 2
end archiveClipboard
archiveClipboard()
I’m not sure if ‘the clipboard as record’ is officially supported or whether it’s just something that just happens to produce the results it does. I’d be inclined to treat it with caution…