Copy image to clipboard?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Copy image to clipboard? Tue Feb 03, 2015 3:47 pm • by f0wlerUK
Hey guys,

Does anyone know of anyway I can use Hazel to copy any images that get added to a folder to get also copied to the clipboard?

I have a current rule that takes any image downloaded, converts it to a PNG, then resizes it and puts the resulting image in a separate folder, but I cannot find a way to also copy the image to the clipboard, so far using Automator within the rule I can only get it to copy the image path not the actual image so it can be pasted directly into another document.

Any ideas?

Paul.
f0wlerUK
 
Posts: 10
Joined: Mon Feb 10, 2014 9:31 am

Re: Copy image to clipboard? Tue Feb 03, 2015 4:30 pm • by gcoghill
How odd, I was just about to ask this exact same question!

For my case, I want to set it to the clipboard so I can then run a Text Expander snippet that will replace a snippet with some text and the file copied to the clipboard into an email reply.

I need the file copied to the clipboard in order to do this. I haven't dug into Automator just yet.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Copy image to clipboard? Wed Feb 04, 2015 11:40 am • by Mr_Noodle
I'm thinking AppleScript is the way to go for this. Try searching around the net as there must be scripts out there to do this.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Copy image to clipboard? Wed Feb 04, 2015 1:52 pm • by gcoghill
[quote="Mr_Noodle"]I'm thinking AppleScript is the way to go for this. Try searching around the net as there must be scripts out there to do from the results I've found discussing this specifically, they suggest using GUI scripting. I'm assuming this will work with Hazel but not sure.

Seems less than elegant but will give it a try when back at the Mac. I'm going to post this question over at the Mac section on Stack Exchange as well.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Copy image to clipboard? Wed Feb 04, 2015 2:22 pm • by gcoghill
I found this solution over on Stack Exchange's Ask Different boards:

Often I find myself at the command line wanting to copy files to the clipboard, so that I can paste them as attachments in Mail. If I have an image file, image1.png, I can accomplish this with

Code: Select all
osascript -e 'tell app "Finder" to set the clipboard to ( POSIX file "image1.png" )'


If I execute the above command and then open a new message in Mail and press command V, the image file is pasted as an attachment. Note that pbcopy is apparently not a solution here, because this would copy the contents of the file and result in junk when pasted with command V.

If ideal for this situation, how would I go about making the script work within Hazel?
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Copy image to clipboard? Thu Feb 05, 2015 12:45 pm • by Mr_Noodle
The script itself is the stuff within the single (') quotes. You would need to replace "image1.png" with theFile, which is the variable that contains the file the rule is matching.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Copy image to clipboard? Thu Feb 05, 2015 2:05 pm • by gcoghill
Seems this must be something that only works from the command line. It complied and ran fine, but just put a generic zero KB document on the Clipboard name "the File".

Digging into the library of the AppleScript Editor for Finder items, the command for Copy says (NOT AVAILABLE YET) which I assume means it isn't going to work as I'd like without going the GUI scripting route.

What a bizarre omission.

Off to try the GUI method now.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Copy image to clipboard? Thu Feb 05, 2015 2:26 pm • by Mr_Noodle
Try theFile with no space or quotes around it.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Copy image to clipboard? Thu Feb 05, 2015 3:39 pm • by gcoghill
That looked more promising, as theFile appeared in green text rather than generic black, but it didn't seem to actually capture anything to the Clipboard.

Nothing pasted, not even the previously copied text string.

*Something* is happening, just not the things we are aiming for.

Thank you.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Copy image to clipboard? Tue Feb 10, 2015 3:53 pm • by f0wlerUK
I've partially found an answer for this, I used the following lines to created an AppleScript:

Code: Select all
on run {input, parameters}
   set thisFile to item 1 of input
   set the clipboard to (read thisFile as TIFF picture)
   return thisFile
end run


And run it through Automator:

Image

Then selected this workflow from Hazel and images now copy to the clipboard fine.

My initial images I wanted copied were PNG's and for some reason these wouldn't copy, but I ran an Automator workflow to convert the image to TIFF first and that works well.

Code: Select all
while read LINE
do
pather=`dirname "${LINE}"`
resulter=`basename "${LINE}" | sed 's/....$//g'`
   /usr/bin/sips -s format tiff "$LINE" --out "${pather}/${resulter}.tiff" &
done


As below:

Image

The only other thing is the AppleScript wouldn't work when I entered it as an embedded AppleScript within Hazel, only when selecting the complied script as an external file, but that's no problem.

Hopefully this may help some others :)
f0wlerUK
 
Posts: 10
Joined: Mon Feb 10, 2014 9:31 am

Re: Copy image to clipboard? Wed Feb 11, 2015 1:10 am • by gcoghill
I was trying to reverse engineer your scipt for my purposes (I need the actual file on the clipboard, not just the contents — so clients can refer to the file name when mentioning which version I emailed to them).

Anyways, I found this and tried it as an embedded script in Hazel and it seems to do what you wanted (the image contents on the clipboard):

Code: Select all
try
   set the clipboard to (read theFile as TIFF picture)
end try


I tried changing it to "JPEG picture" but it still pasted a TIFF image.

This might save you all that workaround. I am still on the hunt for what is an insanely simple task to do with the keyboard (Copy item, Paste item) but seems near impossible to do with AppleScript or Automator. Mind-boggling!

Hopefully that code helps you out.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Copy image to clipboard? Wed Feb 11, 2015 5:01 am • by f0wlerUK
Anyways, I found this and tried it as an embedded script in Hazel and it seems to do what you wanted (the image contents on the clipboard):

Yes excellent stuff, that has helped me to embed the script.

Changing slightly from what you have just shown me, I have since found a way to copy a PNG file rather than it having to be a TIFF, using the following:

Code: Select all
try
   set pngData to read theFile as «class PNGf»
   set the clipboard to pngData
end try


This now works perfectly for me, I found the PNG info over at http://macscripter.net/viewtopic.php?id=38507

I don't know if there is any info over there that will help you get your issue resolved, but thank you very much for helping me, it's much appreciated.
f0wlerUK
 
Posts: 10
Joined: Mon Feb 10, 2014 9:31 am

Re: Copy image to clipboard? Wed Feb 11, 2015 10:22 am • by gcoghill
Glad that was of help. I have a feeling I may need to dig into Automator for this if even possible. It's starting to take more time than it will end up saving! :)
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Copy image to clipboard? Mon Oct 22, 2018 7:13 am • by futuremake.io
For anyone that's arriving here super late, I actually figured out a little bit of a hack. However, unfortunately you need the paid software Keyboard Maestro in order to make it work. If you happen to have those, though, you're golden.

In my particular case, I wanted to immediately copy and paste a video into the app I was running as soon as it hit the Desktop (which in my case was because of the Screenshot app that comes with Mac OSX Mojave). The .mov file needed to be copied to my clipboard and pasted into the second program (called Ulysses).

1. With Hazel, set a rule so that whenever the file is added to Desktop, then show it in Finder. You need to select the extra option of 'Bring to Front' for the action.
2. In Keyboard Maestro, set the action 'Pause Until...', with the condition being triggered when Finder is at the front. In my case, I set cmd+1 to both be the trigger for the macro AND the shortcut for starting a screen recording, so that whenever I went to record my screen the macro would be automatically triggered.
3. When the Finder is at the front, you should make the following actions occur:
1) cmd+C (as the file you've Shown is automatically selected),
2) wait 0.1s,
3) Activate Application [Ulysses],
4) wait 0.1s,
5) cmd+V.

Image

This is how I automated copy and pasting of my .mov file into the Ulysses app specifically.
futuremake.io
 
Posts: 1
Joined: Mon Oct 22, 2018 7:04 am

Re: Copy image to clipboard? Tue Dec 05, 2023 12:39 am • by Paradox
Apologies for necroing an old thread, but I came up with a rather simple solution that should suit your use case nicely.

I used a "Run Shell Script" action, and set the shell to /bin/bash. You then use the following code for the action

Code: Select all
path=$(realpath $1)
osascript -e "on run args" \
          -e 'set the clipboard to POSIX file (first item of args)' \
          -e "end" \
          $path


And you'll find the file on your clipboard, ready to go.
Paradox
 
Posts: 1
Joined: Tue Dec 05, 2023 12:35 am


Return to Support