Jump to content
Bhishan

[SOLVED] Alfred Workflow to copy file contents having space separated folder

Recommended Posts

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 by Bhishan

Share this post


Link to post

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}".

Share this post


Link to post

@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 by Bhishan

Share this post


Link to post

@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.

 

Share this post


Link to post
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
}

 

Share this post


Link to post

@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.

Share this post


Link to post

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.

Share this post


Link to post

@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 by Bhishan

Share this post


Link to post

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

image.png.f760c99e5b9a99dfeb3ceed9a14daf35.png

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...