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

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

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

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.

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

I appreciate your time and help.

Bowjest

Hi,

why not a pure AppleScript action


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

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. :slight_smile: (That is enough for me.)

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

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

Hi StefanK,

I tried your script below:


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

the script is supposed to run in an Automator AppleScript Action

I see. Ok, sorry, my mistake.

I’ll give that a try now.

Many thanks,

Bowjest