Applescript Curl and Variable Help

Joined
Jun 24, 2012
Messages
2
Reaction score
0
Points
1
Hello

I'm mildly experienced with Applescript, but curl and shell confuse me immensely.

I have this snippit of code that just refuses to work.

Now this on it's own works with the script perfectly:
Code:
do shell script "curl -f '" & urlfinish & "' -o '/Users/Luke/Desktop/" & USER & ".png'"

However, when I try to use a dialog box and variable it refuses to work, and interprets the variable as text producing a error:

Code:
tell application "Finder" to set destination to folder (choose folder with prompt "Pick the folder to save image to:")
do shell script "curl -f '" & urlfinish & "' -o 'destination" & USER & ".png'"


Could you help?
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
It's been a while since I have done any Applescript, but I think the problem with your code is, the choose folder command returns an HFS style path as an alias, and you want to pass a POSIX style path as an escaped string to the do shell script command, so you have to convert the returned HFS alias into a POSIX path, and then use the quoted form of command, to convert it into an escaped string, that you then use in the shell script.

Like this example.

Code:
set destinationFolder to choose folder with prompt "Choose the destination folder to save the image file."
set destinationFolderPosixPath to POSIX path of destinationFolder
do shell script "ls " & quoted form of destinationFolderPosixPath

Copy and paste this code into Applescript Editor and run it.

Hope this helps.

Regards Mark
 
OP
F
Joined
Jun 24, 2012
Messages
2
Reaction score
0
Points
1
It's been a while since I have done any Applescript, but I think the problem with your code is, the choose folder command returns an HFS style path as an alias, and you want to pass a POSIX style path as an escaped string to the do shell script command, so you have to convert the returned HFS alias into a POSIX path, and then use the quoted form of command, to convert it into an escaped string, that you then use in the shell script.

Code:
set destinationFolder to choose folder with prompt "Choose the destination folder to save the image file."
set destinationFolderPosixPath to POSIX path of destinationFolder
do shell script "ls " & quoted form of destinationFolderPosixPath

That helped a fair bit yes!

I spent quite a few hours researching how to add folder and things to shell and POSIX seemed the way to go. In the end I came up with this, which works perfectly:

Code:
set the destination to (choose folder)
try
do shell script "curl -L www.startofurl.com/" & USERVARIABLE & "'endofurl.php'" & " -o " & quoted form of ((POSIX path of the destination) & USERVARIABLE & "word" & ".png")
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Well done for getting it working, dont forget the "end try" statement in your code snippet is missing it.
Also consider using an error trap in your try statements, this will help you debug your code much easier, when things go wrong, also you can use the error trap to display a dialog bog to the user, with a useful desciption of the fault.

Like this.

Code:
try
        -- Some code that might cause an error
on error error_message number error_number
	return "Error" & space & error_message & space & error_number as text
end try

And yes your correct, shell scripts only understand POSIX paths, where Applescript commands usually return a HFS colon delimited path, unless otherwise you convert it,
also you need to escape the POSIX path string, in case it contains spaces or other unsupported characters.

Good luck with it.

Regards Mark
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top