My first applescript doesn't work. Please help.

Discussion in 'Mac Programming' started by jbmoore, Sep 15, 2013.

  1. jbmoore, Sep 15, 2013
    Last edited by a moderator: Sep 15, 2013

    macrumors newbie

    Joined:
    Mar 26, 2013
    #1
    I believe that my script doesn't work because i don't understand the
    "document" element or object or property or whatever it's called.
    Obviously, I 'm not going to use this script to just display dialog boxes, I'm actually trying to get firefox to wait for a page to load and then login for me. I already have a script that works but it works by using a 5 second delay and sometimes that seems way to long and other times it's just about right, depending on how fast the page loads. So I guess technically this is my second applescript but the first one was just a copy and paste from a website.

    Even if there is a better way of accomplishing what I want, please explain where I went wrong with my current code, and then if there is a better way, please let me know that as well.

    Here's the current code that I would like to know why it returns false no mater whether i have the page open or not:
    Code:
    [B]tell application "Firefox" to activate
    if exists document "logon.php" then
    	
    	tell application "Finder"
    		display dialog "logon.php exists"
    	end tell
    	
    else
    	tell application "Finder"
    		display dialog "logon.php does not exists"
    	end tell
    end if
    [/B]
    
    I also tried
    
    
    [B]tell application "Firefox" to activate
    if exists document "http://www.somewebsite.com/AircraftScheduling/logon.php" then
    	
    	tell application "Finder"
    		display dialog "logon.php exists"
    	end tell
    	
    else
    	tell application "Finder"
    		display dialog "logon.php does not exists"
    	end tell
    end if
    [/B]
    
    and I've tried

    Code:
    [B]tell application "Firefox" to activate
    if exists "logon.php" then
    	
    	tell application "Finder"
    		display dialog "logon.php exists"
    	end tell
    	
    else
    	tell application "Finder"
    		display dialog "logon.php does not exists"
    	end tell
    end if
    [/B]
    
    The difference on this last one, is I removed the word "document" after "if exists"
    This one always returns true whether I have the page open in a tab or not.
    Which brings up another question. Is there some way of scripting to work with tabs in firefox? I have only saw scripts for windows in firefox, not tabs, so far in my searching the internet.

    Thanks.
     
  2. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    For any application, you will need to use whatever terms are available inside a tell statement that is targeting that application. You can see what terminology is available by opening an application's scripting dictionary in the AppleScript Editor.

    Firefox doesn't have much scripting at all, but you can get the name of the current tab of the front window with something like:

    Code:
    tell application "Firefox"
      set currentPage to name of window 1
    end tell
    
    display dialog currentPage
    Note that you don't need the Finder (or any other application) for the display dialog command.
     
  3. thread starter macrumors newbie

    Joined:
    Mar 26, 2013
    #3
    I used the dictionary in AppleScript Editor to come up with "exists" and "document" but the dictionary doesn't explain, or at least I don't understand, how to use those words.

    Thanks for that script, it did give me the name of the current window, so I changed the part of my code that said "login.php" to the name of the window which is "AircraftScheduling". And it still doesn't work correctly. It will always display the pop-up that says "AircraftScheduling does not exist" when I use:
    if exists document "AircraftScheduling" then whether I have that webpage opened or closed,
    and it will always display the pop-up that says "AircraftScheduling does exist" when I use:
    if exists "AircraftScheduling" then whether I have the webpage open or not.
     
  4. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #4
    Firefox just includes a basic/default dictionary, and although the document term is included, Firefox doesn't do anything with it - for example, you can tell Firefox to get every document, but nothing is returned.

    Your statement if exists "AircraftScheduling" will always return true because you are checking if the string exists (which it obviously does).

    Firefox isn't really that scriptable, so if you are looking at doing something with the page contents, for example interacting with javascript or using a form on the page, Firefox is not the application to use.
     
  5. jbmoore, Sep 15, 2013
    Last edited by a moderator: Sep 15, 2013

    thread starter macrumors newbie

    Joined:
    Mar 26, 2013
    #5
    johnmoore

    Thanks, that makes a lot of since, i shouldv'e known that from my experiences with trying to learn other languages. So I decided to combine the info from both of your replies so that I set the window name to the variable "currentPage" and then see if that variable is = to "Aircraft Scheduling" and if it is then I do nothing, just move to the next line of the script but if it's not then I have a dialog tell me that the window didn't open for some reason and I have the script stop. I also still use some delays here and there to make sure each page has a little time to load before moving forward.

    I tried a few different ways to try to get the script to stop without giving me an error dialogue and this was the only way I found that seemed to work.
    Code:
    	
    	tell application "Firefox"
    		set currentPage to name of window 1
    	end tell
    	
    	if currentPage = "AircraftScheduling" then
    		
    	else
    		display dialog "AircraftScheduling is not open"
    		error number -128
    	end if[COLOR="#808080"]
    
    ----------

    [/COLOR]The last code I just posted seems to be working good for me.
     

Share This Page