Bhishan Posted November 9, 2019 Share Posted November 9, 2019 (edited) I was trying to create an Alfred workflow using which I can copy the contents of file without opening the file. github link of workflow: https://github.com/bhishanpdl/Shared/blob/master/Alfred_questions/2019/aa 00 copy file contents.alfredworkflow?raw=true Fails when: folder name has space in it. Commands used: 1. keyword: copy file contents 2. Run script with osascript ``` tell application "Finder" set someSource to selection as alias list if someSource = {} then return "Select a file in Finder first" end if set theFile to item 1 of someSource as alias return quoted form of the POSIX path of theFile end tell ``` First I had only "return the POSIX path of theFile" then I changed it to "return quoted form of the POSIX path of theFile". It still does not work. 3. Run script ``` cat "$1" | pbcopy ``` This workflow does not work. I googled and found how to get the path of selected item in Finder but could not find how to copy file contents to clipboard. How can this workflow be fixed so that it works for SPACE separated folders? Example of debug: ``` 20:30:00.162] STDERR: aa 00 copy file contents[Run Script] cat: '/Users/poudel/tmp/try space/a.md' : No such file or directory ``` Edited November 11, 2019 by Bhishan Link to comment
deanishe Posted November 9, 2019 Share Posted November 9, 2019 https://forum.latenightsw.com/t/how-do-i-copy-image-file-to-clipboard-and-retain-format/590/27 Bhishan 1 Link to comment
vitor Posted November 9, 2019 Share Posted November 9, 2019 Why are you grabbing the file location via AppleScript invoked by a Keyword, instead of using a File Action Trigger? That’s the best tool for the job. You can even prevent the Workflow from messing up by having the File Action Trigger only work on the +public.text type. On another note, cat "$1" | pbcopy is a useless use of cat. Do pbcopy < "${1}". Bhishan 1 Link to comment
Bhishan Posted November 10, 2019 Author Share Posted November 10, 2019 (edited) @vitor Thanks for suggestion. 1. I used keyword to copy file contents of a file because its easier. using keyword: double tap shift on a file, type copy file contents, paste using file action: hit cmd \ on a file, search workflow copy file contents, select the workflow, paste using keyword saves invoking file action (cmd \) , I can directly use double shift to invoke Alfred command. 2. I appreciate pbcopy < "${1}", this is nicer than cat "$1" | pbcopy. I already have made file action for file copy, it works with space separated folders, but applescript keyword does not work. Edited November 10, 2019 by Bhishan Link to comment
Bhishan Posted November 10, 2019 Author Share Posted November 10, 2019 @deanishe Thanks a lot for suggestion. My attempt was simpler than given link. I am interested in copy text files (.txt, .py, .md etc), images,pdf other types are not required. As vitor, suggested creating file action is super easy just pbcopy < "${1}" works good even for folder with spaces. But, I was attempting to create keyword (I already have file action) since it was easier to use and I didnt need to invoke file action trigger on selected file. I could simply use usual alfred command (double shift) as like in all other cases. Link to comment
vitor Posted November 10, 2019 Share Posted November 10, 2019 12 minutes ago, Bhishan said: using keyword: double tap shift on a file, type copy file contents, paste You may also consider a Hotkey Trigger (set Argument to Selection in macOS). But to solve your question with a Keyword, use osascript (JS) instead of osascript (AS) and this code: try { decodeURI(Application('Finder').selection()[0].url()).slice(7) } catch(error) { error.message } Link to comment
Bhishan Posted November 10, 2019 Author Share Posted November 10, 2019 @vitor I used: javascript ``` try { decodeURI(Application('Finder').selection()[0].url()).slice(7) } catch(error) { error.message } ``` instead of applescript ``` tell application "Finder" set someSource to selection as alias list if someSource = {} then return "Select a file in Finder first" end if set theFile to item 1 of someSource as alias return quoted form of the POSIX path of theFile end tell ``` and got errror: ``` [09:56:44.050] Logging Started... [10:02:46.172] ERROR: aa 00 copy file contents[Run Script] /Users/poudel/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/3E16B543-3ADE-4963-93AE-BC34C6CF3007: line 1: /Users/poudel/tmp/try space/a.md : No such file or directory ``` still does not work with folder with spaces. Link to comment
vitor Posted November 10, 2019 Share Posted November 10, 2019 I see the problem, which is likely what was tripping up your initial code: the string is passed with a newline at the end that is being taken as being part of the name. To fix it, between the Run Scripts add a Transform Utility with Trim Whitespace. Link to comment
Bhishan Posted November 10, 2019 Author Share Posted November 10, 2019 (edited) @vitor This time: 1. runscript applescript ``` tell application "Finder" set someSource to selection as alias list if someSource = {} then return "Select a file in Finder first" end if set theFile to item 1 of someSource as alias return quoted form of the POSIX path of theFile end tell ``` 2. utility transform ``` Transform utitliy trim space ``` 3. runscript ``` pbcopy < "${1}" ``` Error: ``` [10:24:05.982] ERROR: aa 00 copy file contents[Run Script] /Users/poudel/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/B0C62B8C-E0C6-4D51-BEB7-8BB2F90FCCB4: line 1: '/Users/poudel/tmp/try space/a.R': No such file or directory ``` Update: =========== I used another replace utitlity and replaced single quote to double quote: '/Users/poudel/tmp/try space/a.R' ==> "/Users/poudel/tmp/try space/a.R" Still, Alfred debugger says: [10:28:58.588] ERROR: aa 00 copy file contents[Run Script] /Users/poudel/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/7A7A8F20-F2F9-46BF-9D03-BCECF018944E: line 1: "/Users/poudel/tmp/try space/a.R": No such file or directory But, there is a file: /Users/poudel/tmp/try space/a.R In terminal I can type: pbcopy < "/Users/poudel/tmp/try space/a.R" This gives the string: "/Users/poudel/tmp/try space/a.R" and the command: cat "/Users/poudel/tmp/try space/a.R" | pbcopy This gives the actual contents of the file a.R in given directory. and it works in terminal. Edited November 10, 2019 by Bhishan Link to comment
vitor Posted November 10, 2019 Share Posted November 10, 2019 Try this version. It works for me. Bhishan 1 Link to comment
luckman212 Posted November 20, 2019 Share Posted November 20, 2019 Interesting workflow. @vitor's version above works for me too. One caveat I noticed though was that pbcopy seems to choke on data streams with certain encodings. Not sure exactly what's going on but e.g. I have a file that is UTF-16 Unicode - this completely fails to be read in properly by pbcopy (I just get 3 bytes). Has nothing to do with Alfred or the workflow. So, that may be causing you occasional issues @Bhishan Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now