Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
0
Camelot·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
0
eGeekee·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
0
Camelot·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
0
eGeekee·
Imported from the Apple Support Forums via the Wayback Machine 🔗