Applescript if then else problems

Discussion in 'Mac Programming' started by tobylane, Jan 14, 2010.

  1. tobylane macrumors newbie

    Joined:
    Aug 28, 2009
    #1
    It's meant to pass a url between browsers. It errors on the line where the <<< is, with the error of 'Expected "then", etc, but found identifier. What can I do? Plus, as I'm new to this any general ideas I lack in?
    Code:
    tell application "System Events"
    	delay 1
    	--Change Opera and Safari for your two browsers.
    	set b1 to "Opera"
    	set b2 to "Safari"
    	say b1
    	set myApp to name of first application process whose frontmost is true
    	if myApp is "b1" then
    		say yes
    		tell application b1
    			set myInfo to GetWindowInfo of window 1
    			set myURL to item 1 of myInfo
    		end tell
    	else if myApp is "Safari then
    		tell application b2
    			set myURL to URL of document 1
    		end tell
    	else
    	say else
    	return
    	end if
    	if myApp is "b1" then               <<<<<
    		tell application b2
    			OpenURL myURL
    		end tell
    	else if myApp is "b2" then
    		tell application b1
    			make new document with properties {URL:theURL}
    		end tell
    	else
    		say "test2"
    		return
    	end if
    end tell
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Two problems I see initially

    Code:
    else if myApp is "Safari then
    Missing a quote.

    Code:
    say else
    Not sure what that's supposed to be, but "else" is undefined.
     
  3. tobylane thread starter macrumors newbie

    Joined:
    Aug 28, 2009
    #3
    Of course. I didn't expect it to support multiline names or not to say if there's a quote mismatch.

    'System Events got an error: can't get application "Safari"' from line ' set myURL to URL of document 1'

    Also (when that if part isn't run), Opera is apparently triggered by OpenURL Var, but that or OpenURL "address" isn't accepted by the editor, 'Expected end of line but found identifier' highlighting the variable.
     
  4. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    This is a source of a lot of frustration with AppleScript -- not detecting quote failures. Unlike lots of languages, a string in AppleScript can be most anything, including several paragraphs. So it becomes a tradeoff. You gain the greater flexibility on strings, but you also have to know that when you see some errors, it means you quoted improperly.

    Speaking of which:

    I don't believe "b1" and "b2" need to be in quotes here. You have created vars b1 and b2 to represent the names of your browsers. Putting them in quotes means you're telling AS that you want those two strings, "b1" and "b2" and not the values they represent.

    mt
     

Share This Page