New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Applescript Curl and Variable Help


Post Reply New Thread Subscribe

 
Thread Tools
FallenAsh

 
Member Since: Jun 24, 2012
Posts: 2
FallenAsh is on a distinguished road

FallenAsh is offline
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?
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks
FallenAsh

 
Member Since: Jun 24, 2012
Posts: 2
FallenAsh is on a distinguished road

FallenAsh is offline
Quote:
Originally Posted by Mark FX View Post
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")
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks

Post Reply New Thread Subscribe


« Python and cv2 | AppleScript - I get a {} when trying to separate items into categories. »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Scripting a cron job to post data to a url via variable lizardthefish OS X - Development and Darwin 4 02-14-2004 04:33 PM

All times are GMT -4. The time now is 10:47 PM.

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

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?