Jump to content

Copying and Renaming a File with Clipboard Contents


Recommended Posts

Hi. I'm trying to create a workflow that renames a folder on my desktop called "Catalog - Aaron Riddle Photography Template" to the current contents of my clipboard. 

 

  1. I begin the workflow with a query to input.
  2. The input is copied to my clipboard contents.
  3. However, when I try to execute the following script to rename the folder, it doesn't work:
mv '/Users/aaronriddle/Desktop/Catalog - Aaron Riddle Photography Template' '/Users/aaronriddle/Desktop/{query}'

Note if I use that script and change the name of the file to a generic name, like "Test Folder", it works:

mv '/Users/aaronriddle/Desktop/Catalog - Aaron Riddle Photography Template' '/Users/aaronriddle/Desktop/Test Folder'

So is {query} the right thing to use here? Screenshot also attached.

 

Thank you for any help you can provide!!!

Screen Shot 2020-08-23 at 6.52.54 PM.jpg

Link to post

Just tested this, and double quoting the second argument should fix the problem. Change your script object as follows (changing "[folder name]" to the target folder's name):

mv "$HOME/Desktop/[folder name]" "$HOME/Desktop/{query}"

 

Equivalently, if for whatever reason you change the script object's input tab to "with input as argv":

query=$1
mv "$HOME/Desktop/[folder name]" "$HOME/Desktop/$query"

 

Here's some information regarding quoting in bash:

Quote

3.1. 2.2 Single Quotes
Enclosing characters in single quotes (' ' ') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

 

3.1.2.3 Double Quotes
Enclosing characters in double quotes (' " ') preserves the literal value of all characters within the quotes, with the exception of ' $ ', ' ` ', ' \ ', and, when history expansion is enabled, ' ! '. When the shell is in POSIX mode (see Bash POSIX Mode), the ' !

 

Last note: "$HOME" is a special bash variable that automatically inserts your home folder into a double-quoted string. In general, be cautious when posting information that may identify you or your machine online.

 

 

Edited by Dattwood
clarification
Link to post
8 minutes ago, Dattwood said:

Just tested it, and double quoting the second argument does fix the problem. Change your script object as follows:

 

You should always use double quotes because Alfred doesn't offer the necessary escaping options to work correctly with single quotes.

 

9 minutes ago, Dattwood said:

Equivalently, if for whatever reason you change the input tab to "with input as argv":

 

Though “With input as argv” should always be preferred. It doesn’t have any of the issues {query} has.

 

10 minutes ago, Dattwood said:

Please don't ever post your real home folder information online!

 

It's hardly that much of an issue (especially on a client machine). @afordturtle already included his real name in his post.

Link to post
22 minutes ago, Dattwood said:

But good general advice to live by, no?

 

Certainly, but you made it sound like it was really bad.

 

I'm all in favour of putting the fear of God into people over information security, but when you say "don't ever do that" to something that isn't that big a deal, it doesn't leave you a whole lot of room to express how much worse things you genuinely should never do are (like post an API key online or save your login password in a script).

 

You know what I mean?

Link to post

Wow thanks for the help! I appreciate it! One last question...how would I adjust the second argument to append a constant prefix (for instance "Job Log" to the front of the folder name prior to {query}? So if my query was "Test Folder"...it would create the folder as "Job Log - Test Folder".

 

Something like this (which doesn't seem to work):

 

mv "$HOME/Desktop/[folder name]" "$HOME/Desktop/Job Log - {query}"

 

Link to post
30 minutes ago, afordturtle said:

Something like this (which doesn't seem to work):

 

That's exactly how you should do it. When something doesn't work, check Alfred's debugger to see what's going wrong. And post the debugger output when you ask for help on the forum.

Link to post

Here is the whole workflow I am trying to create. It works as follows:

  1. Takes an already created template folder, copies it to the Desktop, and renames it to the query with "Catalog - " in front of it.
  2. Then creates a new folder on the desktop with the query as the folder name, and four sub folders within.

All of this works great, aside from when the template folder is created and renamed, it inserts "\" in front of where spaces appear in the query. Here is the workflow script...and also a screenshot showing it and the folder output below.

 

In this example, the query "123 Test" was used.

 

cp -a "$HOME/Dropbox (Personal)/Aaron Riddle Photography/Lightroom Settings/Catalog Templates/Catalog - Aaron Riddle Photography Template" "$HOME/Desktop/"

mv "$HOME/Desktop/Catalog - Aaron Riddle Photography Template" "$HOME/Desktop/Catalog - {query}"

cd $HOME/Desktop; mkdir {query}; cd {query}; mkdir Originals; mkdir Finals; mkdir Blog; mkdir Release;

 

Screen Shot 2020-08-24 at 12.38.24 PM.jpg

Link to post
1 hour ago, afordturtle said:

it inserts "\" in front of where spaces appear in the query

 

You’re using incorrect Escaping options. Like I said earlier, don’t use {query}, use “with input as argv”. It’s much simpler and therefore much harder to screw up.

 

Change it to "with input as argv" and use the following script:

query="$1"
cp -a "${HOME}/Dropbox (Personal)/Aaron Riddle Photography/Lightroom Settings/Catalog Templates/Catalog - Aaron Riddle Photography Template" "${HOME}/Desktop/Catalog - ${query}"
mkdir -p "${HOME}/Desktop/${query}"/{Originals,Finals,Blog,Release}

 

Link to post
7 hours ago, afordturtle said:

Awesome. That works perfectly! Thank you for your help. Sorry...I'm a scripting newbie :)

 

Getting the Escaping options right for {query} is difficult for anyone, tbh. That's why argv was added.

 

{query} can’t be removed for backwards-compatibility reasons, unfortunately.

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