You want to imitate Finder’s ability to put file references on the clipboard? If you do not want to see Finder windows you will never get there with UI scripting. How about a way to emulate copying a file to the clipboard that does not involve Finder at all?
Here are the notes from some exploration I just did regarding clipboard info,the cliboard as .,set the clipboard to . (all from the StandardAdditions OSAX) and how Finder copies files:
- Copy a file with Finder. I am using a PDF file from my user’s home directory.
- Examine the clipboard info to see what Finder makes available when it copies a file.
clipboard info --> {{string, 13}, {Unicode text, 26}, {«class hfs », 80}, {«class utf8», 13}, {«class ut16», 28}, {picture, 2616}, {«class icns», 43336}, {«class furl», 43}}
- Eliminate the text representations. Even if they represent the path to the file, most applications will just see it as normal text when pasted.
set {s, u, u8, u16} to {the clipboard as string, the clipboard as Unicode text, the clipboard as «class utf8», the clipboard as «class ut16»}
--return {class of s, class of u, class of u8, class of u16} --> {string, Unicode text, Unicode text, Unicode text} -- This is Tiger. Leopard will probably be different
{s = u, u = u8, u = u16} --> {true,true,true} -- They are all stringy and all have the same characters (the name of my file had only ASCII characters).
- Eliminate the image representations. Finder seems to copy the (non-˜preview’) icon of the file to the clipboard. Sometimes nice, but useless for our purposes.
display dialog "Please copy a file with Finder (Edit > Copy)."
set pic to the clipboard as picture
set icns to the clipboard as «class icns»
set the clipboard to pic
display dialog "Use Preview's File > New From Clipboard command to examine the picture object" --> Preview shows me a 32x32 RGB image
set the clipboard to icns
display dialog "Use Preview's File > New From Clipboard command to examine the «class icns» object" --> Preview shows me a icon set 2 grayscale: 16x16, 32x32 and 5 RGB: 128x128, 32x32, 16x16, 32x32, 16x16 (yes, 16 and 32 are repeated twice; oh well, we do not really care about this part anyway)
- This leaves «class hfs » and «class furl». Since they are shown as raw class references, neither of these have proper dictionary names. «class furl» objects present themselves as file objects in Script Editor, but it is probably not as simple as it seems since the compiler does not rewrite «class furl» as file like it does for many other raw class references. Technically this probably means that these are supposed to be for internal use only. Any result we end up with here will officially be unsupported (no matter if it works anyway). Count on it breaking in some future release (maybe even Leopard, I have not tested there). Anyway, «class furl» looks like a good avenue to investigate.
display dialog "Please copy a file with Finder (Edit > Copy)."
set furl to the clipboard as «class furl»
return furl --> file "HFS:Path:To:My:File"
set the clipboard to furl
clipboard info --> {{«class hfs », 80}, {«class furl», 43}}
Hey, look at that, we put in a «class furl» and we ended up with an additional «class hfs ». So, they are probably related and it would seem that we do not need to worry about figuring out what «class hfs ». Maybe this ˜copy one and get the other as a bonus’ feature is even symmetrical.
- So if we can find a way to create our own «class furl» objects ˜from scratch’, we can probably emulate the interesting part of Finder copying a file. From experience, I think POSIX file will create them and we can probably coerce some other classes to «class furl». Putting it all together, I ended up with this:
-- Get references to a particular file
set fileAlias to choose file with prompt "Choose a file to copy to the clipboard:" default location (path to home folder)
set filePosixPath to POSIX path of fileAlias
set fileHFSPath to fileAlias as Unicode text
-- Use call of these, depending on which kind of object you have. Be sure to use the my if the call is in an application tell block.
my copyPosixPathFileToClipboard(filePosixPath)
my copyHFSPathFileToClipboard(fileHFSPath)
my copyAliasToClipboard(fileAlias)
to copyPosixPathFileToClipboard(posixPath)
-- the POSIX file object specifier seems to create «class furl» objects
copyFurlFileToClipboard(POSIX file posixPath)
end copyPosixPathFileToClipboard
to copyHFSPathFileToClipboard(hfsPath)
-- the "as «class furl»" coercion seems to be able to treat strings and unicode text as HFS paths
copyFurlFileToClipboard(hfsPath as «class furl»)
end copyHFSPathFileToClipboard
to copyAliasToClipboard(fileAlias)
-- the "as «class furl»" coercion seems to be able to handle aliases
copyFurlFileToClipboard(fileAlias as «class furl»)
end copyAliasToClipboard
to copyFurlFileToClipboard(furlFile)
if class of furlFile is not «class furl» then error "Expected a «class furl» object."
set the clipboard to furlFile
end copyFurlFileToClipboard
- Test the copy operation by pasting into a target application. I tried TextEdit (which embeds the PDF file I was selecting) and Preview (which opened the PDF). It works for me.
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 3.2.1 (4525.27.1)
Operating System: Mac OS X (10.4)