Pasting a file to the clipboard (not it´s content)

Dear all,

I tried to find an answer on the search engine and at Google but am unable to solve this.

I would like to set the clipboard to a certain file type on the harddisk. Think of MP3s or ZIP files or whatever. Similar to the copy / paste function you can use in the Finder by pressing Apple+C and Apple+V.

I know that it is possible to send the keystrokes, but this doesn´t do the trick for me as I try to get rid of the popping up Finder windows.

This is pretty close to what I would like to do, but I would like to be able to use every file instead of just pictures, so that it would be possible to press Apple+V manually on a e.g. USB stick to copy the file on the clipboard from it´s location to the stick.

tell application "Image Events" to ¬
    save (open alias "path/to/myfile.jpg") as PICT in "/tmp/f"

set the clipboard to (read ("/tmp/f" as POSIX file) as picture)

Thanks in advance!

Is there something more to this than what you have shared so far?

If you do not want to have a Finder window open when you set the
clipboard to the selection then you must already have the path to
that item. If this is so why use the clipboard? If this is not the
case please explain in more detail exactly what you are wanting
to accomplish.

Regards,

Craig

Automated Xcode Backups

Yes, in my idea I get the path of the file name as a POSIX path from iTunes.

That´s where I am at the moment:

 
tell application "iTunes"
	set trackList to the selection of window 1
		if ((count trackList) is 0) then
			return
			else if ((count trackList) is greater than 1) then
			return
		end if
	
repeat with theTrack in trackList
	set theTrackName to (the location of theTrack)
		tell application "Finder"
			activate
			select theTrackName
		end tell
		tell application "System Events"
			tell application process "Finder"
				keystroke "c" using command down
				keystroke "w" using command down
			end tell
		end tell
	end repeat
end tell

As you can see it will open a Finder window at the path which is passed to theTrackName by iTunes. I would like to get around this opening finder window. :frowning:

To stop Finder from opening a window you will need to remove these lines.


       tell application "Finder"
           activate
           select theTrackName
       end tell

As you can see here. You are telling the Finder to activate which brings to
front a window if open or it spawns a new window. Then you are telling
Finder to select that item in that window. You are effectively using a
Reveal in Finder call.

If you will post what it is that you are trying to accomplish I think we
can show you a better way. Are you are wanting to copy selected
files to a USB drive? That is a different question than how to copy
music files to the clipboard.

Regards,

Craig

Automated Xcode Backups

Well, sorry if I am not providing enough infos to give me a sense-making answer.

I´ll do my best now to explain a bit further but I think everything somebody must know is already in this thread.
The USB stick thing was just an example on how to proceed further as I already investigated a bit on my own (sucessfully) and tried to compress the facts so that it is not so time-consuming.

The MP3 file in the clipboard should be pasted to Ableton Live (which hasn´t got any applescript capabilitys) by sending a “Apple+V” to the application. When I press Apple+C on the MP3 which is selected in Finder and switch to Ableton Live - it works perfectly fine. What I try to do in the end is controlling Itunes via MIDI by implementing this applescript in an application called “MIDIPipe” so that I can “paste” a selected track from Itunes to Ableton Live.

Last open step is to hide the popping up Finder window to get it a bit nicer. The rest is already working.

Thank you, again! Really appreciate your help.

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:

  1. Copy a file with Finder. I am using a PDF file from my user’s home directory.
  2. 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}}
  1. 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).
  1. 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)
  1. 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.

  1. 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
  1. 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)

Simply, just WOW WOW WOW!

Thank you soo much for you effort! I will have to read through all this a 2nd and a 3rd time but I got how you did it in general.

Impressing how much time you spent to help me with this. Great job and good to know that there are people who really know their stuff!

Thx again. I´ll definitly comment back if I got it working that way…

It´s working!

Great!

From what I found on the internet, you´re the first who got this working. I found tons of similar threads and nobody got a solution for it. Really great thank you for your work!!!