"if" and "else" statements in AppleScript

I was wondering how to run script based on "if" or "else" statements. I want my program to check a URL for me and run different script based on that URL. For example, my program opens my site and gets redirected to one of two pages. I could then have the program copy and paste the URL (unless there's a better way to do this) into a text box and based on which URL it is then it will choose a script to run.

So, if it is returned as "www.mysite.com/123/" then it would run (insert code 1) but if it is anything other than that URL then it would run (insert code 2)

Is there anyone that can explain to me how to do this or link me a tutorial explaining how to do this? Thank you.

G4 iMac, Mac OS X (10.3.x), busted disk drive too lazy to fix

Posted on May 23, 2010 8:12 PM

Reply
5 replies

May 24, 2010 6:07 PM in response to eGeekee

This worked for me using Mac OS 10.4.11 and Camino v. 2.0.2:

*tell application "Camino"*
activate
*try -- script exits gracefully if no window is open*
*set my_window to window 1*
*set my_url to URL of my_window*
*if my_url = "http://www.mysite.com/123/" then*
*beep -- do code 1 stuff here*
else
*beep 2 -- do code 2 stuff here*
*end if*
*end try*
*end tell*

The beep commands were included as substitutes for code 1 and 2 stuff, for testing purposes only. Hope this helps.

May 23, 2010 8:21 PM in response to eGeekee

This is relatively easy to do in AppleScript - in fact, you've already written it.

In AppleScript, an if/then/else statement looks like this:

if (condition) then
-- code here if condition is true
else
-- code here if condition is false
end if


So assuming you've ascertained your URL and stored in in the variable MyURL then your code would look something like:

if MyURL = "http://www.mysite.com/123/" then
-- code here for 'true'
else
-- code here for false
end if

May 23, 2010 10:54 PM in response to Camelot

That is extremely simple, thank you. However I'm still unclear as to how to make AppleScript grab the URL and initiate the "if MyURL" statement based on that. I tried looking it up but haven't had success trying to code it.

Could you (or someone else) please explain that to me? The best I know how to do is make the application copy and paste the URL using keyboard commands in the browser window.

May 24, 2010 8:31 AM in response to eGeekee

I'm still unclear as to how to make AppleScript grab the URL and initiate the "if MyURL" statement


Well, the first question there is what app are you using?
The specific syntax may vary depending on the application.

For example, if you're using Safari and you want the URL of the current/frontmost window then you can use something like:

tell application "Safari"
set myURL to URL of document 1
end tell


but other applications might be a little different.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

"if" and "else" statements in AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.