0

This started because I wanted to be able to paste screenshots or pictures copied from Safari into Finder folders. Here are the steps to get to where I am:

  1. Install pngpaste using brew install pngpaste.

  2. Make Automator Quick Action, and configure as:

enter image description here

osascript -e 'set formattedDate to (do shell script "date +'%Y-%m-%dat%H-%M-%S%p'")' -e 'tell application "Finder" to set thePath to the quoted form of (POSIX path of (insertion location as alias) & formattedDate & ".png")' -e 'if ((clipboard info) as string) contains TIFF picture then do shell script "/usr/local/bin/pngpaste " & thePath' -e 'if ((clipboard info) as string) does not contain TIFF picture then tell application "System Events" to keystroke "v" using control down'
  1. Save and close.
  2. Remap default paste:

enter image description here

  1. Assign default paste to trigger service:

enter image description here

THE ISSUE: No matter what type of file I have in my clipboard, the script always detects it as a TIFF and pastes it as a PNG. How do I fix this?

1
  • Just an FYI on your issue and presumably why your own workflow didn't produce the desired results… when I run 'clipboard info' on a PNG (from a screen shot) I get all manner of image types in the result — including TIFF. It's not actually detecting that it's a TIFF, it's just finding that string (along with GIF, BMP and JPEG and others). I think it's an issue of how the clipboard deals with binary data but not really sure. – Mockman Dec 22 '20 at 2:46
1

Having to write and maintain an osascript command as you have is really not the best way to go in this use case IMO.

Using a Run AppleScript action, instead of a Run Shell Script action, the following example AppleScript code will resolve the issue you are currently having, and makes the code much easier to read and edit:

on run {input, parameters}
    
    set cbInfoAsString to (clipboard info) as string
    
    if cbInfoAsString does not contain "«class furl»" and ¬
        cbInfoAsString contains "TIFF picture" then
        
        set formattedDate to do shell script ¬
            "date -j '+%Y-%m-%d at %I.%M.%S %p'"
        
        tell application "Finder" to set thePath to ¬
            (insertion location as alias) & ¬
            formattedDate & ".png" as string
        
        do shell script "/usr/local/bin/pngpaste " & ¬
            the quoted form of the POSIX path of thePath
        
    else if cbInfoAsString contains "«class furl»" then
        
        tell application "System Events" to ¬
            keystroke "v" using control down
        
    end if
    
end run

Notes:

  • POSIX path is a part of Standard Additions not Finder, and should not be wrapped within a tell statement of Finder.
  • If not setting the date with the date command, use the -j option.
  • I modified your date command to use 12-Hour Time as typically 24-Hour Time does not use AM/PM and have added made additional modifications to make it more readable, and in line with the system default used with screen shots.
  • If you want 24-Hour Time then use e.g.: "date -j '+%Y-%m-%d at %H.%M.%S'"
  • As coded, if there is anything other than a file(s)/folder(s) or just an image, the script will not attempt to process text if that's what's on the clipboard.
7
  • This works flawlessly. Thank you very much! – Landon Dec 19 '20 at 14:04
  • @Landon, RE: "This works flawlessly." -- Considering your intended usage, i.e. the insertion location returned by Finder you have read/write permissions, it should work without issue. However, as there is no error handling included in the code it can be problematic if the current insertion location is not writable, e.g. Recents or Tags is selected in Finder, or Machintosh HD. I just though it was worth mentioning. – user3439894 Dec 19 '20 at 17:08
  • A more direct approach that negates the need for ‘pngpaste` would be to write (the clipboard as "PNGf") to f as "PNGf" (after confirming clipboard info for ”PNGf” ≠ {}, and creating the file close access (open for access f) where f is the posix path to write out to). Likewise, rather than key press a paste, one can either read the contents of the furl file object and write it out as before; or, use Finder or System Events to duplicate the file from the path stored in the furl object itself to the insertion location. [ I realise these are the preserved parts of the OP’s code ] – CJK Dec 27 '20 at 0:58
  • @CJK, The intent of my answer was to simply clean up the OP's existing coding and methodology as I didn't have time to delve into it any further at that point in time. The bottom line is, there usually is more than one way to do anything and there is almost always a better way to do the same thing. Take my answer compared to the other one in the other question you commented too just less than an hour ago at this point. :) – user3439894 Dec 27 '20 at 1:07
  • I guess it wasn’t as obvious as I thought it was, but the whole point of me adding “I realise these are the preserved parts of the OP’s code” was so you wouldn’t have to justify yourself, as they are clearly additional notes primarily for the OP and/or anyone who cares. I understood what your answer was aiming to offer and where it was consciously putting a ceiling on how far outside the actual question it was intending to consider as extra credit. We both know you know your stuff and you know if it’s for you I’m more “Why do it like that?!” – CJK Dec 27 '20 at 1:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .