Simple Script give errors

I use safari to look at a text file. The URL to the file is [url=http://HTTP://www.yadda.com/yadda/order_123.txt]HTTP://www.yadda.com/yadda/order_123.txt[/url] and it is a text file.

So I just want to save it to my local machine. I wrote


tell application "Safari"
	save document 1 as "secure_123.txt" in beginning of alias "Macintosh HD:Documents:Secure Orders:"
end tell

And something this simple does not work. Any clues. Applescript is so frustrating.

Is this something you are trying to automate because if you are viewing the page in Safari why not just use “Save Page as…”???

What is the error you are getting?

I use to:

do shell script "cd " & quoted form of (posix path of alias "Macintosh HD:Documents:Secure Orders:") & "; curl -O " & quoted form of "[url=http://HTTP://www.yadda.com/yadda/order_123.txt]HTTP://www.yadda.com/yadda/order_123.txt[/url]"

Your save command’s parameters are all wrong. (Perhaps you’re confusing it with Finder scripting?)

On 10.2, a design flaw in Cocoa Scripting’s default Standard Suite means that the save command in many Cocoa apps requires a posix path string, e.g.:

tell application "Safari" to save document 1 in "/Users/has/foo.html"

I believe this is fixed in 10.3 to use the traditional file specification/alias object:

tell application "Safari" to save document 1 in file "Macintosh HD:Users:has:foo.html"

I suspect the ‘as’ parameter is still a useless piece of suck, but just ignore it.

HTH

Well, it still does not work. Is there ANY standard syntax in applescript! or do you have to know syntax for every different script you write?

It has little to do with AppleScript syntax, and a lot to do with how different applications implement - and document - their scripting interfaces. To script an application, you first have to learn how its scripting interface works.

Unfortunately, application dictionaries don’t provide nearly enough information on what sorts of values should be passed to commands, and many applications don’t bother supplying additional documentation to fill in the holes. Half the job is figuring out what the missing info is - whether by intuition, trial-and-error, studying other scripts or asking on a forum like this one - and that’s before you get to any application-specific bugs, quirks, mis-features, etc. (Yes, this sucks. Feel free to submit feature requests with the relevant developers. The more they hear about it, the more likely they are to fix it.)

Anyway, I suggest you post your new non-working code, say what OS you’re on and describe the error raised.

I’m in OSX 10.3.7
Here is the script so far:
tell application “Safari”
activate
open location “https://location.com/yadda/secure_123.txt”—this works
delay 10
tell application “Safari” to save document 1 in file “Macintosh HD:documents:orders:secure_123.txt”—it does not save the document

end tell

Well you don’t need to nest the ‘tell app “Safari”’ blocks, but I doubt that’s the problem. Put it this way: it should work, but with application scripting you can never be certain. Could be a bug in Safari’s save command, or could be continued stupidity in how it’s implemented [1].

Unfortunately, I’m still on 10.2 so can’t really help any further, but maybe another 10.3 user can help you with it. FWIW though, having seen your script, if all you want to do is download a webpage to disk the you’d be better off using curl as-per jj’s suggestion anyway. (Unless you want to save the page as plaintext, not html, in which case there’s a different way you’ll need to do it.)

HTH

[1] Checking Safari’s dictionary on macscripter, I see the ‘in’ parameter’s type listed only as alias, so perhaps it’s refusing to take a file spec in its place. Even though you need a file spec to refer to non-existent files, since aliases can only refer to existing files.

Well, it is a text file I’m trying to save. The location I get the file from is secure (HTTPS) and requires a log-in. Since safari memorizes the log-in password this is pretty easy to navagate to that text file. I do not know how to do that with curl. IE, pass the user name and password to the protected directory and retreive it securly since it contains credit card information.

I previously did this easily with fetch, and thought the commands would be the same. But when I went to safari to do it securly, it blew apart. In fetch, the path looked like:

copy remote file secureorder to beginning of alias “Macintosh HD:Documents:Secure Orders:”

If it is https authentication, usually you can add login into to the same url. Eg: “http://user:password@www.site.com/path/file.txt

Hi,

When I want to get Safari text, I use this script:

tell application “Safari”
set the_text to text of front document
end tell
set conv_text to LF_To_CR(the_text)
tell application “Tex-Edit Plus”
launch
activate
make new document at front
set text of front document to conv_text
end tell
---------- subroutines
– Replaces LF with CRs
on LF_To_CR(the_text)
set cur_tid to AppleScript’s text item delimiters
try
set AppleScript’s text item delimiters to {ASCII character 10}
set paragraph_list to text items of the_text
set AppleScript’s text item delimiters to {return}
set new_text to paragraph_list as string
set AppleScript’s text item delimiters to cur_tid
on error err_mess
set AppleScript’s text item delimiters to cur_tid
display dialog err_mess buttons {“OK”} default button “OK”
error err_mess
end try
return new_text
end LF_To_CR
---------- end subroutines

From Tex-Edit Plus, I examine the text to see if it needs further modification. Sometimes the html page will contain returns (or line feeds) after every line. Tex-Edit Plus deals with this rather well. I use the script:

tell application “Tex-Edit Plus”
strip line endings front document removing cr with line length 1
end tell

Internet paragraphs usually are displayed in blocks with blank lines as separators so this script doesn’t remove the separators.

gl,