Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 15, 2013, 11:50 AM   #1
jbmoore
macrumors newbie
 
Join Date: Mar 2013
My first applescript doesn't work. Please help.

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:
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


I also tried


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
and I've tried

Code:
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
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.

Last edited by maflynn; Sep 15, 2013 at 02:30 PM. Reason: Added code tags
jbmoore is offline   0 Reply With Quote
Old Sep 15, 2013, 01:36 PM   #2
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
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.
__________________
MacBook Pro / OS X Yosemite (10.10) / Xcode 6.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Sep 15, 2013, 02:22 PM   #3
jbmoore
Thread Starter
macrumors newbie
 
Join Date: Mar 2013
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.
jbmoore is offline   0 Reply With Quote
Old Sep 15, 2013, 02:55 PM   #4
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
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.
__________________
MacBook Pro / OS X Yosemite (10.10) / Xcode 6.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Sep 15, 2013, 05:17 PM   #5
jbmoore
Thread Starter
macrumors newbie
 
Join Date: Mar 2013
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

----------

The last code I just posted seems to be working good for me.

Last edited by maflynn; Sep 15, 2013 at 06:01 PM. Reason: code tags added
jbmoore is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Black screen , HDMI doesn't work , VGA doesn't work Madridista Mac Basics and Help 4 Jun 23, 2014 06:45 PM
Applescript, look after file don't work why? Dirtyharrydev Mac Programming 1 Jan 17, 2014 10:06 PM
VPN by iPhone work but at MacBook doesn't work! why ? Jingol Mac Basics and Help 1 Sep 12, 2013 05:40 PM
Why doesn't this simple applescript work any more in ML mail.app (as a rule) wesleyh Mac Programming 0 Jan 26, 2013 10:25 AM
Freemyapps doesn't work SilentFisher iPad Tips, Help and Troubleshooting 1 Jul 23, 2012 09:06 AM

Forum Jump

All times are GMT -5. The time now is 07:12 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC