2

I'm trying to copy multiple files to the clipboard in macos like this:

./file2clip.applescript /User/Cool/Dekstop/test.txt /User/Cool/Dekstop/myfolder

I already can do this with only one file:

#!/usr/bin/osascript
on run args
    set the clipboard to POSIX file (first item of args)
end

But it only works on one file...

I have tried this

on run args
    set the clipboard to POSIX files args
end

But It didn't work.

And also this

on run args
    set pathList to {}
    repeat with arg in args
        set end of pathList to POSIX file arg
    end repeat
    set the clipboard to pathList
end

But this also didn't work

Adding all the POSIX files to a string also doesn't work because the clipboard will have only text inside it so I can't paste all the files with ctrl + v, I could only paste their names. Not what I want to achieve.

property files: ""

on run args
    repeat with f in args
        set files to files & POSIX file f & "\n"
    end repeat
    set the clipboard to files
end

Any ideas?

lordcommander
  • 320
  • 3
  • 7
  • Note that you are saving a _list_ to the clipboard. – red_menace May 22 '21 at 13:19
  • @red_menace Yes, it doesn't work because of that, I couldn't think of anything else. Have any ideas? – lordcommander May 22 '21 at 13:20
  • Depending on exactly what output you are wanting, the result can be coerced to text before being placed on the clipboard. – red_menace May 22 '21 at 13:30
  • @red_menace No i'm not looking for text, I want to copy a 'links' to the files so then I can paste them with `cmd` + `v` wherever I want. – lordcommander May 22 '21 at 13:32
  • `POSIX file` refers to a file object specified with a POSIX pathname, but it isn’t a link - are you wanting URLs? – red_menace May 22 '21 at 14:23
  • @red_menace I said link because i didn't know how to call it, `POSIX file` is what i want. Multiple POSIX files – lordcommander May 22 '21 at 14:24
  • You are already passing POSIX files to the script. If you are only placing those on the clipboard, just set it to a string instead of a list (coerce the list, separate the items with returns, etc), otherwise you can edit your question to include more specific details. – red_menace May 22 '21 at 14:50
  • @red_menace can you show me how to put it as a string with many files? – lordcommander May 22 '21 at 14:55

1 Answers1

0

In your original snippets, setting the clipboard to the first argument item works because it is text. Setting the clipboard to a list appears to fail because it is a list - the data is there, it just doesn't work the same as text (clipboard info can be used to get information about items on the clipboard).

From the command line, arguments (quoted and/or escaped as needed) should already be passed on to the script as a list of POSIX paths, but coercions and/or manipulations such as tilde expansion can also be performed as needed. The individual items can be placed onto the clipboard however you want using regular text operations - for example, to separate the arguments with returns, you can just do something like:

on run args
    set pathString to ""
    repeat with arg in args
        set pathString to pathString & arg & return
    end repeat
    set the clipboard to pathString
end run
red_menace
  • 3,162
  • 2
  • 10
  • 18
  • After running the script, I tried to use `ctrl` + `v` on the desktop and no file was pasted. I think your code just copied their names into the clipboard... Not what i'm looking for. But thanks anyways – lordcommander May 22 '21 at 16:17
  • I've been trying to figure out _exactly_ what you are wanting to do, and from the comments, narrowed it down to POSIX paths on the clipboard. It would be up to your script to do whatever it is you are trying to do with the paths passed to it - duplicate, open, copy contents, etc, but I _still_ don't know what that is. – red_menace May 22 '21 at 16:34
  • I'm trying to be able to run `./myscript.applescript file.txt file2.txt`, Then go to my desktop, press `ctrl` + `v` on my keyboard, and see file.txt file2.txt appear. – lordcommander May 22 '21 at 16:41
  • How are you determining the destination path? Note that the "desktop" is a Finder construct, so it knows where it is - shell scripts need to be told or have some other way to figure that out. Is there a particular reason for not using the copy/paste from the Finder? – red_menace May 22 '21 at 16:47
  • I don't want to know the destination path... only copy to clipboard, so I can paste the file with `ctrl`+`v` in the desktop, or in any other folder – lordcommander May 23 '21 at 08:52
  • The clipboard doesn’t work like that - an application provides that kind of functionality. Even the Finder file copy/paste doesn't put the file itself onto the clipboard, it just provides an edit menu item that uses the file url in certain situations. Your script would need to provide that file paste functionality, which without a UI would not be the normal control+v. So you are wanting to provide complete file paths for the script via the command line, that get copied to the front Finder window? Or it gets complete file paths from the clipboard to copy somewhere? – red_menace May 23 '21 at 15:27
  • Please run the first code block in **my** question like so: `./file2clip.applescript /User/YourUser/Desktop/file.txt`. I can't seem to explaain to you my question so here you can see for yourself. This works for one file as I said. What i'm trying to achieve is making this work with multiple files – lordcommander May 23 '21 at 16:24
  • I had mentioned file URLs in an earlier comment but I guess you didn’t understand me. The Finder copies file URLs to the clipboard to use if you are pasting files, so your script needs to do the same thing. [This answer](https://apple.stackexchange.com/a/339436/321333) seems to be what you are looking for. – red_menace May 23 '21 at 17:12
  • I had mentioned file URLs in an earlier comment, but it sounded like this was a part of something else, so I was trying to figure out what that was. – red_menace May 23 '21 at 17:28