Thursday, November 23, 2017

#1 2013-07-17 10:38:54 am

bowjest
Member
Registered: 2010-08-31
Posts: 166

Using more than one variable in Automator

I'm trying to create an Automator application to download a file whenever this is needed.

Here is what I have so far (and it works, but not like I want):

1. Ask for text
This is where the path to the file on the internet is given

2. Run shell script
curl "$@" -o outputFIle.txt

The problem is, however, that I want to be able to give the file a name on the fly, but have no idea how to pass more than one variable to Automator ("$@").

Can anyone advise further? I've tried using "Set value of variable" and "Get value of variable", but I can't figure out how to stack these and then use the actual variable names in the curl script.

Many thanks,

Bowjest

Offline

 

#2 2013-07-17 12:00:59 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Using more than one variable in Automator

Hello.

$@ actually means every variable, in double ticks in shell lingo.

If you were sure ther wasn't any space in the filenames, and I'm pretty sure that if there were any, they'd be replaced by %20 in the filenames, then you could pass two parameters like this: http://mcusr.net/index.html bloat.txt like two parameters, in the curl line you'd reference the two with $1 $2: $1 for index.html and $2 for bloat. (curl $1 -o $2)

I hope this make sense, they act like, and are  regular shell parameters, so maybe you should have a look in the bash manual. (man bash in a terminal window, or google it).

Last edited by McUsrII (2013-07-17 12:01:49 pm)

Offline

 

#3 2013-07-18 02:17:47 am

bowjest
Member
Registered: 2010-08-31
Posts: 166

Re: Using more than one variable in Automator

Thanks, McUsrII. That helps a lot. I seem to remember something about this now (I haven't had to script anything for several years, so very rusty).

The problem now, however, is getting the bash command to receive the variables correctly.

Here is what I've done since your message:

1. Ask for text
Enter path to file

2. Ask for text
Assign name for file

3. Run shell script
curl "$1" -o "$2".txt

This, however, fails. The message is: "Run Shell Script failed - 1 error", but it doesn't say what the error is.

I'm assuming that the problem is how I'm presenting the variables to the bash command. Do I need to use some sort of Set variable/Get variable parameter? Otherwise, aren't the Automator actions just run/parsed from top to bottom, i.e. the 2. Ask for text is being viewed by curl as being "$1" and there is no "$2" to work with?

Any advice would be most welcome.

Bowjest

Offline

 

#4 2013-07-18 03:53:43 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Using more than one variable in Automator

Hello.

I was more thinking that you had the filename entered after the url, maybe it is a way to do like you want to, but I think I have come up with an easier solution, since you want interactivity.

If you want both the url, from a text file, AND interactivity, then maybe Automator isn't the right solution.

Maybe you will want to make an application with an applescript action  instead.

Offline

 

#5 2013-07-18 04:06:05 am

bowjest
Member
Registered: 2010-08-31
Posts: 166

Re: Using more than one variable in Automator

Thanks, I think you're right. I'll give AppleScript a try.

I appreciate your time and help.

Bowjest

Offline

 

#6 2013-07-18 04:08:11 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Using more than one variable in Automator

Hi,

why not a pure AppleScript action

Applescript:


on run {input, parameters}
   set theURL to text returned of (display dialog "Enter URL" default answer "http://")
   set outputFile to choose file name
   do shell script "curl -o " & quoted form of POSIX path of outputFile & space & quoted form of theURL
   return input
end run

Last edited by StefanK (2013-07-18 04:22:26 am)


regards

Stefan

Offline

 

#7 2013-07-18 04:16:30 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Using more than one variable in Automator

Hello.

I was thinking more of a pure applescript that gets the url from the window of the frontmost application while it is run from the script menu. If you want shortcut keys, then you can download the free version of FastScripts that gives you 20 some short-cuts to scripts. smile (That is enough for me.)

Applescript:

on run
   tell application id "com.apple.systemEvents"
       
       set procname to name of every application process whose frontmost is true and visible is true
   end tell
   tell application (procname as text)
       activate
       tell application id "com.apple.systemEvents"
           keystroke "c" using command down
       end tell
   end tell
   
   set theUrl to the clipboard
   set failed to false
   try
       set filename to quoted form of POSIX path of (choose file name)
   on error
       set failed to true
   end try
   
   
   if not failed then
       do shell script "curl " & theUrl & " -o " & filename
   end if
end run

Edit
It is 10 scripts you can have a short cut key to. That'll hold for my purposes, and spare me some agony, relating to scripting AppleScript from within Automator, besides that, I think I have a home-rolled solution for short-cut keys and scripts. smile

I really recommend FastScripts for those that doesn't have it. After it is running and properly placed in an application or utilities folder, you fire up AppleScript editor, and hook away the "Show Script menu" in its preferences pane.

Last edited by McUsrII (2013-07-18 05:28:38 am)

Offline

 

#8 2013-07-18 04:21:52 am

bowjest
Member
Registered: 2010-08-31
Posts: 166

Re: Using more than one variable in Automator

Thanks, StefanK and McUsrII.

That's super. I really appreciate your help with this.

I'll have a look at both options when I finish work today.

All the best,

Bowjest

Offline

 

#9 2013-07-18 08:12:33 am

bowjest
Member
Registered: 2010-08-31
Posts: 166

Re: Using more than one variable in Automator

Hi StefanK,

I tried your script below:

Applescript:


on run {input, parameters}
   set theURL to text returned of (display dialog "Enter URL" default answer "http://")
   set outputFile to choose file name
   do shell script "curl -o " & quoted form of POSIX path of outputFile & space & quoted form of theURL
   return input
end run

But I get an error message as follows:

error "" number -1721

I don't really understand what that means.

The script seems to work fine if I remove the opening and closing "run" as well as the "return input".

Is it complaining about how the file is referenced in some way?

Bowjest

Offline

 

#10 2013-07-18 09:12:28 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Using more than one variable in Automator

the script is supposed to run in an Automator AppleScript Action


regards

Stefan

Offline

 

#11 2013-07-18 09:17:43 am

bowjest
Member
Registered: 2010-08-31
Posts: 166

Re: Using more than one variable in Automator

I see. Ok, sorry, my mistake.

I'll give that a try now.

Many thanks,

Bowjest

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)