Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy the GIF directly to the clipboard #2

Open
kejadlen opened this issue Jun 27, 2016 · 56 comments
Open

Copy the GIF directly to the clipboard #2

kejadlen opened this issue Jun 27, 2016 · 56 comments

Comments

@kejadlen
Copy link
Owner

No description provided.

@kejadlen
Copy link
Owner Author

Dun.

@invious
Copy link

invious commented Nov 14, 2016

How? I am holding CMD before pressing enter and the gif is not copying to the pasteboard?

@kejadlen
Copy link
Owner Author

Ah, I screwed it up. New release with the fix: https://github.com/kejadlen/giphy.alfredworkflow/releases/tag/v2.0.2

@invious
Copy link

invious commented Nov 14, 2016

I saw you changed the filter variable check from {var:copy} to {var:giphy_copy}, that's what i was going to try to see if you had made a mistake. but even with that fix I am having trouble and cannot get it to work. the modifier key is working, because I substituted the script with a notification, but the gif isn't copying to pasteboard

@kejadlen
Copy link
Owner Author

Ah, I was afraid of that. There should be a binary in the workflow called imgpbcopy but I wasn't sure if it would work on other peoples' machines. It takes a path to an image and should put it on the pasteboard. Can you see if running that directly works?

@invious
Copy link

invious commented Nov 14, 2016

the binary didn't work but using the swift interpreter did

./imgpbcopy icon.png
dyld: Library not loaded: @rpath/libswiftAppKit.dylib
  Referenced from: /Users/aymon/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.C4B633AD-6EC5-4616-A71C-E9AD1E8D4EAE/./imgpbcopy
  Reason: image not found
Abort trap: 6
Aymons-MacBook-Air:user.workflow.C4B633AD-6EC5-4616-A71C-E9AD1E8D4EAE aymon$ swift imgpbcopy.swift icon.png
Aymons-MacBook-Air:user.workflow.C4B633AD-6EC5-4616-A71C-E9AD1E8D4EAE aymon$

@kejadlen
Copy link
Owner Author

You can probably compile the Swift script yourself to make it work, but I don't know how to distribute it to work for everyone, so... ¯_(ツ)_/¯

@invious
Copy link

invious commented Nov 15, 2016

Yeah, dunno. I didn't compile it I just changed it to run the script with the interpreter

@kejadlen
Copy link
Owner Author

Maybe I'll switch it to that for now, thanks for the update.

@kejadlen kejadlen reopened this Nov 27, 2016
@kejadlen
Copy link
Owner Author

Re-opening to figure out a way to make this actually work.

@invious
Copy link

invious commented Nov 27, 2016

when I got it to work the script was only copying the image to the NSPasteBoard as PNG, (not animated GIF) so yeah

@kejadlen
Copy link
Owner Author

@invious I saw that you forked the workflow - does paste.swift work? I couldn't get that to work.

It would probably help if I knew how to copy an animated gif to the clipboard just in general...

@invious
Copy link

invious commented Nov 29, 2016

@kejadlen it used to work! some update must have broken my code. the date of those releases, the code was working. I remember looking far and wide for pasting gif to the keyboard, i can try to look again. Maybe there are some hints in my broken code.

@invious
Copy link

invious commented Nov 29, 2016

@kejadlen I think I fixed it! Takes the url as an argument.

import Cocoa

var url_arg = CommandLine.arguments[1]

var pb = NSPasteboard(name: NSGeneralPboard)

if let url = NSURL(string: url_arg) {
    if let data = NSData(contentsOf: url as URL) {
        var image =  NSImage(data: data as Data)
        let fileName = url.lastPathComponent
        var saveURL = NSURL(string: "file://" + NSTemporaryDirectory())
        saveURL = saveURL?.appendingPathComponent(fileName!) as NSURL?
        let data = NSData(contentsOf: url as URL)
        data?.write(to: saveURL! as URL, atomically: true)

        pb.declareTypes([NSFilenamesPboardType], owner: nil)
        pb.writeObjects([saveURL!])

    }
}

@kejadlen
Copy link
Owner Author

kejadlen commented Nov 30, 2016

@invious What OS and version of Swift are you using? This is what I'm trying, which should be pretty much the same as what you're doing, but it's not working for me:

let pasteboard = NSPasteboard.general()
pasteboard.clearContents()
pasteboard.declareTypes([NSFilenamesPboardType], owner: nil)
let url = URL(fileURLWithPath: "/absolute/path/to/gif.gif")
pasteboard.writeObjects([url as NSPasteboardWriting])

When I paste this, I still get a still image.

@invious
Copy link

invious commented Dec 6, 2016

Xcode 8, Swift 3.0.1 I don't know but the code I pasted works. Here is my working workflow: https://cl.ly/2T2m1E0m3l0y

@invious
Copy link

invious commented Dec 12, 2016

Did it work for you?

@kejadlen
Copy link
Owner Author

kejadlen commented Jan 1, 2017

Gave it a shot, but it doesn't work for me.

@mlaurel
Copy link

mlaurel commented Sep 22, 2017

Works for me... just so people know, these are the steps I'm doing...

  1. Initiate Alfred
  2. Type giphy and search parameter, i.e. "giphy beer"
  3. When you get a list of results in Alfred, use your arrow keys to arrow down to the one you want
  4. DON'T PRESS ENTER... instead, with the list item highlighted, do a CMD+C
  5. Paste in your app

@invious
Copy link

invious commented Sep 24, 2017

I still can't get this to work?

@kejadlen
Copy link
Owner Author

Ditto - that only copies the link to the gif for me.

@aliparr-
Copy link

Hello. I fixed the swift so that copying works.
Code is here:
https://github.com/aliparr-/giphy.alfredworkflow/releases/tag/v3.0.4

@invious
Copy link

invious commented Sep 30, 2017

@aliparr- I just installed your new release, and holding the cmd modifier gives a GIF copied notification but when I paste into iMessage, it just pastes the url. God I swear I got my fork to work at one point. I distinctly remember joyfully pasting Gifs into imessage from alfred >_<

@kejadlen
Copy link
Owner Author

Tracking some of the research I'm doing:

Copying a GIF from Safari:

let pb = NSPasteboard.general
let item = pb.pasteboardItems?.first
for type in item!.types {
    print(type.rawValue)
}
public.tiff
dyn.ah62d4rv4gu8zs3pcnzme2641rf4guzdmsv0gn64uqm10c6xenv61a3k
dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu
public.url
public.url-name
public.utf8-plain-text
com.apple.flat-rtfd
com.apple.webarchive

I'm able to paste the animated GIF into TextEdit, but it doesn't seem to work for Slack/Chrome.

@kejadlen
Copy link
Owner Author

Some other notes:

  • clearContents needs to be called first before writing to the pasteboard.
  • writeObjects with a class that implements NSPasteboardWriting is probably the way to go, if we can identify what type should go on the pasteboard and what format it should be in. This is actually ideal, since then this workflow could in theory support copying URLs/HTML/images in one shot, though there is the open question of whether the URL should be the file URL (for local use) or the web URL (for copying for remote use).

@aliparr-
Copy link

aliparr- commented Oct 1, 2017

@invious - That's really weird to me. I have it working perfectly here. Would you mind running the alfred debugger on the script and seeing what it throws up?

@invious
Copy link

invious commented Oct 2, 2017

@aliparr-

[2017-10-01 20:27:47][ERROR: action.script] ./imgpbcopy.swift:16:9: error: function 'pb' was used as a property; add () to call it
        pb.declareTypes([NSPasteboard.PasteboardType.fileContents], owner: nil)
        ^
          ()
./imgpbcopy.swift:17:9: error: function 'pb' was used as a property; add () to call it
        pb.writeObjects([saveURL!])
        ^
          ()

Swift 3.1

Welcome to Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42). Type :help for assistance.

Oh and actually I was wrong about it copying the url, that was the previous thing on my clipboard, so I mistakenly thought it was the result of running the script with the CMD modifier, but nothing gets put on the clipboard despite the notification

@kejadlen
Copy link
Owner Author

kejadlen commented Oct 2, 2017

@aliparr- How are you testing that copying the animated gif works?

@aliparr-
Copy link

aliparr- commented Oct 2, 2017

@kejadlen - I can paste it where I want. iMessage, whatsapp, anywhere. It's working 100% for me. Can you try updating Swift and giving my version a go?

@kejadlen
Copy link
Owner Author

kejadlen commented Oct 3, 2017

@aliparr- Interesting... can you check to see what the following returns when you do declareTypes and then writeObjects?

let item = pb.pasteboardItems?.first
for type in item!.types {
    print(type.rawValue)
}

@aliparr-
Copy link

aliparr- commented Oct 4, 2017

I get this sort of thing:
"dyn.ah62d4rv4gu8y60cgrf0gnu5tr34gn5xysrkge55bsmwfk8puqy"

@kejadlen
Copy link
Owner Author

kejadlen commented Nov 4, 2017

Did some further digging into this today. Got this to work:

import Cocoa

let path = CommandLine.arguments[1]
let url = URL(fileURLWithPath: path).absoluteURL as NSURL

let pb = NSPasteboard.general
pb.declareTypes([NSPasteboard.PasteboardType.fileContents], owner: nil)
pb.writeObjects([url])

@kejadlen
Copy link
Owner Author

kejadlen commented Nov 5, 2017

Okay, new release that hopefully implements direct copying of GIFs: https://github.com/kejadlen/giphy.alfredworkflow/releases/tag/v3.1.1

There is still some odd behavior that I haven't been able to nail down - it seemed like it sometimes only worked after I ran the script from the commandline, so that's something to look out for.

@olivierlefloch
Copy link

This failed for me because

  1. The app imgpbcopy was from an unauthorized developer
  2. I guess I don't have the Swift runtime?
dyld: Library not loaded: @rpath/libswiftAppKit.dylib
  Referenced from: /Users/alakazam/Library/Mobile Documents/com~apple~CloudDocs/Alfred/Alfred.alfredpreferences/workflows/user.workflow.0F3E13E1-4435-447A-AAE4-D5F376DE070C/imgpbcopy
  Reason: image not found

@kejadlen
Copy link
Owner Author

One thing you can try is compiling imgpbcopy yourself in the workflow folder. The behavior is still kind of flaky, but I personally don't use it a whole lot so I haven't done a ton of work on it.

@heyflorin
Copy link

Fixed it on my end. Here are the issues I found.

  1. This version of imgpbcopy has some issues, couldn't even compile it. Downloaded version from here: https://gist.github.com/ibolmo/c08973f5d4534ae2ff13f38e02f9e286 and added to Workflow folder.
  2. Modified final script that called imbpbcopy. Notice the "./" and the new variable call:
    ./imgpbcopy "$giphy_url"
  3. (Optional) Removed final qlmanage view since quick I quick preview every gif before I copy so I don't need to see it again.

Works.

@invious
Copy link

invious commented Jan 26, 2019

@florinme do you have a working fork? I couldn't quite follow your instructions. I downloaded, chmoded, and added the imgpbcopy file to my path but you lost me at step 2

@kejadlen
Copy link
Owner Author

So out of curiousity, what are y’all pasting animated GIFs into?

@heyflorin
Copy link

@florinme do you have a working fork? I couldn't quite follow your instructions. I downloaded, chmoded, and added the imgpbcopy file to my path but you lost me at step 2

See my screenshot; that's the script to modify with the code above.
image

image

@heyflorin
Copy link

So out of curiousity, what are y’all pasting animated GIFs into?

Apple Messages. Often times I don't have the text field highlighted, and so returning the gif directly didn't work for me, which is why I'd rather control when the output happens.

@invious
Copy link

invious commented Feb 14, 2019

Apple Messages

ditto

@miguelarios
Copy link

@florinme do you have a working fork? I couldn't quite follow your instructions. I downloaded, chmoded, and added the imgpbcopy file to my path but you lost me at step 2

See my screenshot; that's the script to modify with the code above.
image

image

I tried what you did but could not get it to work. Out of curiosity the imgpbcopy does work as I ran this via terminal with a Giphy link; however, I noticed that how the Giphy url is copied through this workflow it doesn't work via terminal. For example, this is the url for 'hello world' https://media1.giphy.com/media/lcs5BL0NIM4WMv61a9/giphy.gif?cid=e1bb72ff1e937d0c558ace37ce09ebd2dd622bb6557907b3&rid=giphy.gif. I can't have the gif copy to clipboard via the imgpbcopy unless I remove everything after the '?'.

@kejadlen
Copy link
Owner Author

It appears to be working for me with what's currently in the workflow (osascript -e "set the clipboard to (read (POSIX file \"$alfred_workflow_cache/giphy.gif\") as JPEG picture)"), FWIW.

@miguelarios
Copy link

It appears to be working for me with what's currently in the workflow (osascript -e "set the clipboard to (read (POSIX file \"$alfred_workflow_cache/giphy.gif\") as JPEG picture)"), FWIW.

Did you change the imgpbcopy?

@kejadlen
Copy link
Owner Author

kejadlen commented Jan 4, 2020

@mar005 I have the relevant portion of imgpbcopy inlined in the Run Script block, which is how the current release is configured.

@wilfredwee
Copy link

wilfredwee commented Jan 8, 2020

Do we need the new code from December in order to get copy to work? I'm having trouble actually copying the image myself, pressing enter just shows the qlmanage preview.

Edit: Ah, pressing Cmd + Enter successfully copies the gif. Thanks!

@aaronvegh
Copy link

I came across this thread while trying to support this feature in my own app. I finally managed it using this code: perhaps you'll be able to benefit?

let pb = NSPasteboard.general
        do {
            let fileType = try NSWorkspace.shared.type(ofFile: localPath.path)
            let pasteboardType = NSPasteboard.PasteboardType.init(fileType)
            pb.declareTypes([pasteboardType], owner: nil)
            pb.writeObjects([localPath as NSURL])
        } catch {
            return
        }

@bryanbartow
Copy link

@aaronvegh Are you copying remote gifs from a URL or local files in the bundle? Do you have a more full code sample?

@aaronvegh
Copy link

In my application's case, I'm copying URLs, but this handles both local and remote URLs. Not sure if this will be helpful for you, but here are two methods from my app that handle copying images that handle the image file in one instance, and the URL in another:

@IBAction func copyOriginalURL(sender: NSMenuItem) {
        guard let gif = gif,
              let originalURL = gif.url else { return }
        // put it on the clipboard
        let pasteboardItem = NSPasteboardItem()
        pasteboardItem.setString(originalURL, forType: .string)
        
        let pasteboard = NSPasteboard.general
        pasteboard.clearContents()
        pasteboard.writeObjects([pasteboardItem])
    }
    
    @IBAction func copyOriginalImage(sender: NSMenuItem) {
        guard let localPath = localPath else { return }
        
        let pb = NSPasteboard.general
        do {
            let fileType = try NSWorkspace.shared.type(ofFile: localPath.path)
            let pasteboardType = NSPasteboard.PasteboardType.init(fileType)
            pb.declareTypes([pasteboardType], owner: nil)
            pb.clearContents()
            pb.writeObjects([localPath as NSURL])
        } catch {
            return
        }
    }

@bryanbartow
Copy link

@aaronvegh This copies the URL to the clipboard, but not the .gif file itself, right? I'm attempting to copy the .gif from my app and paste directly into another app, i.e. Messages.

@aaronvegh
Copy link

I find this works to do exactly what you're after. For the "actual file", you're really wanting to put the URL in the pasteboard. In a different part of my app where I have to return an object that conforms to NSPasteboardWriting (to support drag 'n' drop), I return an NSURL object.

@bryanbartow
Copy link

@aaronvegh Interesting. When I do this and go to paste in Messages, it just pastes the remote URL of the file.

@elisavetsky
Copy link

It seems as though what we copy is just a JPEG file, the gif is running fine on my mac when I paste it into iMessage, but when i switch over to my phone's iMessage, it reads as a JPEG picture and doesn't animate. Huh???

@xeijix
Copy link

xeijix commented Feb 15, 2022

FWIW: the applications where I need to be able to paste a gif works if I had a gif on my filesystem and I copied the file through finder via the context menu.

Since that works, I tried updating the script in the workflow to just set the clipboard to the tmp file (instead of reading it in)

osascript -e "set the clipboard to POSIX file \"$alfred_workflow_cache/giphy.gif\""

And that works for me. YMMV, I didn't test iMessages or other common applications people might use for messaging

@avoll89
Copy link

avoll89 commented May 27, 2022

I'm having this issue too (@kejadlen I got it to stop crashing! I didn't have python 2 installed 🤦). It only copies a static image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests