Jump to content

How can I copy a file to my clipboard with passed in finder path?


Recommended Posts

I use a workflow that will reveal my recently taken screenshots in Alfred. By default it will reveal the screenshot in finder, but I also want to be able to paste the screenshot on action. I added a "copy to clipboard" block to be trigged with the command modifier and when I use it it copies the path to the clipboard instead of the file. I can't seem to figure out how to copy the FILE to the clipboard instead of the path. I can file action it within Alfred and copy the file to the clipboard and that does work but I can't seem to figure out how to do it from within an Alfred workflow to be triggered by a branched connection with a modifier. After all that is faster & I'd rather do it that way. How can I do this?

 

2058358182_ScreenShot2022-01-06at9_34_34AM.png.bdb25f1a36f34bf83f03484c762af2df.png

 

Thanks!

Link to comment
3 hours ago, Undertaker01 said:

How can I do this?

 

You need to put a file:/// URL on the clipboard with data type public.file-url. Here is a script (Language = "/usr/bin/osascript (JavaScript)") that will put the first file passed in on the clipboard:

 

ObjC.import('AppKit');

function run(argv) {
	const pboard = $.NSPasteboard.generalPasteboard;
	// convert first command-line argument to file:/// URL
	let url = $.NSURL.fileURLWithPath(argv[0]);

	// put URL on clipboard as "public.file-url"
	pboard.clearContents;
	if (!pboard.setStringForType(url.absoluteString, $.NSPasteboardTypeFileURL)) {
		throw new Error('failed to put URL on pasteboard');
	}
}

 

3 hours ago, Undertaker01 said:

2058358182_ScreenShot2022-01-06at9_34_34AM.png.bdb25f1a36f34bf83f03484c762af2df.png

 

Could you stop posting screenshots of Alfred and your workflows all the time? It's pointless.

Link to comment
8 minutes ago, Undertaker01 said:

Is that because it's using the odd way of formatting?

 

What odd way of formatting?

 

Look, you need to be specific about what you're doing and what is/isn't happening. I can't tell you why something isn't pasting properly in an application when I have no idea what you're copying or which application you're pasting it into.

 

10 minutes ago, Undertaker01 said:

doesn't paste the image

 

It's a file path, not image data. They're not the same thing.

 

If pasting an image into some graphics program is what you're trying to do, then you should have said so.

Link to comment
1 hour ago, deanishe said:

If pasting an image into some graphics program is what you're trying to do, then you should have said so.

 

Ideally I'd want something that would work for pasting into finder or a graphics program. I asked if I could copy the actual image to the clipboard instead of the finder path of the image. One would think there'd be something built into Alfred's workflow items that could achieve that, after all there's a built in file action for it, I don't know.

Link to comment
5 hours ago, Undertaker01 said:

I asked if I could copy the actual image to the clipboard instead of the finder path of the image.

 

Well that certainly isn't clear from your first post:

 

11 hours ago, Undertaker01 said:

how to copy the FILE to the clipboard instead of the path.

 

Link to comment
ObjC.import('AppKit');

function run(argv) {
	let pboard = $.NSPasteboard.generalPasteboard,
		path = argv[0],
		image = $.NSImage.alloc.initWithContentsOfFile(path);

	pboard.clearContents;
	if (!pboard.setDataForType(image.TIFFRepresentation, $.NSPasteboardTypeTIFF)) {
		throw new Error('failed to put image on pasteboard');
	}
}

 

Link to comment
41 minutes ago, deanishe said:
ObjC.import('AppKit');

function run(argv) {
	let pboard = $.NSPasteboard.generalPasteboard,
		path = argv[0],
		image = $.NSImage.alloc.initWithContentsOfFile(path);

	pboard.clearContents;
	if (!pboard.setDataForType(image.TIFFRepresentation, $.NSPasteboardTypeTIFF)) {
		throw new Error('failed to put image on pasteboard');
	}
}

 

 

Thanks so much!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...