Check if I'm on the right page

I’m trying to make a script that writes out the title of a video clip I’m watching at Youtube. For this I’m trying to make a statement that will check if I’m surfing on youtube.com.

Having some troubles, because I’m not sure how to do it.

A part of the script:


	if theContent = "*.youtube.com/*" then
		
		set theContent to «class pTit» of myFirefox
		set theString to "/me is watching " & theContent
		
	end if

It seems like you cannot use asterisks… I guess RegExp?
Unfortunatly I have no idea how, so please help me!

And another thing, Youtube puts there name in the page title, is there some way to remove it… Regexp? :stuck_out_tongue:

You probably don’t need regular expressions for what it seems you are after. See if this does what you want:

set theURL to "http://fake.youtube.com/fake-video-reference"
set theTitle to "YouTube - Fake Title"

set prefixToRemove to "YouTube - "
set prefixLen to length of prefixToRemove
if theURL contains ".youtube.com/" then
	if length of theTitle > prefixLen ¬
		and characters 1 thru prefixLen of theTitle as string is equal to prefixToRemove then
		set theTitle to characters (prefixLen + 1) thru -1 of theTitle as string
	end if
end if
log theTitle

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Thankyou! It works perfect!

Until…

The script is for the IRC app Linkinus. For it to work i have to add “on linkinuscmd()” and “end linkinuscmd” in the beginning and end of the script. After doing this I get the error number -1708. Not sure if this is something you guys can work out, but I give it a try!

Here’s the entire code:

on linkinuscmd()
	set firefox_active to false
	set theString to "/me doesn't watch anything on Youtube right now"
	
	tell application "Finder"
		if (get name of every process) contains "firefox-bin" then set firefox_active to true
	end tell
	
	if firefox_active then
		
		set got_url to false
		
		tell application "Firefox"
			if (count of windows) > 0 then
				set myFirefox to properties of front window
				set got_url to true
			end if
		end tell
		
		if got_url then
			set theContent to «class curl» of myFirefox
			
			if theContent contains "youtube.com" then
				set theTitle to «class pTit» of myFirefox
				set prefixToRemove to "YouTube - "
				set prefixLen to length of prefixToRemove
				
				if length of theTitle > prefixLen ¬
					and characters 1 thru prefixLen of theTitle as string is equal to prefixToRemove then
					set theTitle to characters (prefixLen + 1) thru -1 of theTitle as string
				end if
				
				set theString to "/me is watching " & theTitle & " at Youtube"
			end if
		end if
		
	end if
	
	set theResult to theString
end linkinuscmd

You notice something wrong?

Once again chrys, thanks!

Model: Macbook
AppleScript: 1.10.7
Browser: Firefox 2.0.0.4
Operating System: Mac OS X (10.4)

Your script from post #3 works OK for me when I run it from Script Editor. I did get various errors when I had the preferences window open, and when the add-ons window was open. When either of those windows were frontmost in Firefox, the script would get an error when it tried to get the properties of front window.

Error -1708 means “ doesn’t understand the message.” I actually got one of these once while trying out the script at first, but I have not been able to replicate it. The error I got was for count of windows, though I do not have any good ideas why that would be causing problems (I think it was saying that Firefox did not understand the count message).

If the script is mostly being run OK from your IRC program, but occasionally produces an error (because you have a non-browser window up in Firefox?), you might try wrapping most of the code in a try block. That way any errors produced would be ignored:

on linkinuscmd()
	set firefox_active to false
	set theString to "/me doesn't watch anything on Youtube right now"
	
	try
		with timeout of 10 seconds
			tell application "Finder"
				if (get name of every process) contains "firefox-bin" then set firefox_active to true
			end tell
			
			if firefox_active then
				
				set got_url to false
				
				tell application "Firefox"
					if (count of windows) > 0 then
						set myFirefox to properties of front window
						set got_url to true
					end if
				end tell
				
				if got_url then
					set theContent to «class curl» of myFirefox
					
					if theContent contains "youtube.com" then
						set theTitle to «class pTit» of myFirefox
						set prefixToRemove to "YouTube - "
						set prefixLen to length of prefixToRemove
						
						if length of theTitle > prefixLen ¬
							and characters 1 thru prefixLen of theTitle as string is equal to prefixToRemove then
							set theTitle to characters (prefixLen + 1) thru -1 of theTitle as string
						end if
						
						set theString to "/me is watching " & theTitle & " at Youtube"
					end if
				end if
			end if
		end timeout
	end try
	
	set theResult to theString
end linkinuscmd

I also added a with timeout block in there because I noticed a problem where the script would get stuck if I had opened the directory chooser for the “Save files to” preference in the Main preference tab. The AppleScript’s default timeout is 60 seconds and the script lowers that 10 seconds to prevent it from taking forever to update in the case you have a window open that causes Firefox to be non-responsive to AppleScript.

BTW, I was testing with Firefox 2.0.0.4.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)