Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   OS X - Development and Darwin (http://www.mac-forums.com/forums/os-x-development-darwin/)
-   -   Applescript Curl and Variable Help (http://www.mac-forums.com/forums/os-x-development-darwin/292622-applescript-curl-variable-help.html)

FallenAsh 01-03-2013 06:50 PM

Applescript Curl and Variable Help
 
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?

Mark FX 01-04-2013 12:01 PM

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

FallenAsh 01-04-2013 03:04 PM

Quote:

Originally Posted by Mark FX (Post 1484282)
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")


Mark FX 01-06-2013 01:59 PM

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


All times are GMT -4. The time now is 08:10 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.