Results 1 to 4 of 4
  1. #1
    Applescript Curl and Variable Help

    Member Since
    Jun 24, 2012
    Posts
    2
    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?

  2. #2
    Applescript Curl and Variable Help
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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

  3. #3
    Applescript Curl and Variable Help

    Member Since
    Jun 24, 2012
    Posts
    2
    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")

  4. #4
    Applescript Curl and Variable Help
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. how to insert variable's value?
    By LauM86 in forum OS X - Development and Darwin
    Replies: 4
    Last Post: 07-16-2013, 09:38 AM
  2. Restoring curl
    By JoeLiberté in forum OS X - Operating System
    Replies: 1
    Last Post: 04-12-2013, 10:41 AM
  3. Set variable to output of text field- xcode 4.5, applescript
    By dhmmjoph in forum OS X - Development and Darwin
    Replies: 15
    Last Post: 02-03-2013, 04:13 PM
  4. save container variable to file in applescript?
    By psychodork in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 03-09-2009, 01:41 AM
  5. environment variable needs to be changed
    By chris_101010 in forum OS X - Operating System
    Replies: 5
    Last Post: 09-25-2006, 12:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •