Strange compiler error with remote apple event - Safari

This will compile without problems:


set urlList to {}

tell application "Safari" of machine "eppc://192.168.178.20"
	set numTabs to count tabs of window 1
	repeat with x from 1 to numTabs
		set newURL to URL of tab x of window 1
		set urlList to urlList & newURL
	end repeat
end tell

But I get an error when I use the machine address in a variable:


set urlList to {}
set remMachine to "eppc://192.168.178.20"

tell application "Safari" of machine remMachine
	set numTabs to count tabs of window 1
	repeat with x from 1 to numTabs
		set newURL to URL of tab x of window 1
		set urlList to urlList & newURL
	end repeat
end tell

The compiler complains about the variable x in the line:
set newURL to URL of tab x of window 1
Error: Expected end of line but found identifier

Maybe I made a mistake with the syntax in this line. But it works in the first example. I’m pretty confused. :confused:

Model: MBP Late 2007 Snow Leo
AppleScript: 2.1.2
Browser: Safari 4.0.5 (6531.22.7, 533+)
Operating System: Mac OS X (10.6)

Hi,

you need an using terms from block to resolve the terminology


set urlList to {}
set remMachine to "eppc://192.168.178.20"

tell application "Safari" of machine remMachine
	using terms from application "Safari"
		set numTabs to count tabs of window 1
		repeat with x from 1 to numTabs
			set newURL to URL of tab x of window 1
			set urlList to urlList & newURL
		end repeat
	end using terms from
end tell

Thanks! That worked.

I use this script to get the open tabs/windows in Safari from one Mac to the other.
Desktop <-> Laptop for example

If someone needs it or wants to improve it (I guess I didn’t always use the easiest way). :wink:

Here it is:


(*
System Preferences -> Sharing -> Remote Apple Events
	must be activated on the remote machine (the one you want to get
	the tabs from)

Change the properties below to the IP of the remote machine
and the username on the remote machine.

Then either save the script in UserFolder/Library/Scripts/Applications/Safari/
(This will make it possible to allow automatic Keychain Access.)
And run it from the script menu icon while you are in Safari.

Or just save it anywhere as an application.
(But you will need to enter the password every time the script is used.)

BTW: If someone got a solution how to sign (google ACL osx)
applescript apps so they can access the Keychain automatically
please let me know here:
http://macscripter.net/viewtopic.php?id=32903
*)

property ipNumber : "192.168.1.10"
property userName : "harry"


global remMachine
global urlList
set remMachine to "eppc://" & userName & "@" & ipNumber
set urlList to {}


(*
other possible uses:
set remMachine to "eppc://username:password@192.168.1.10"
(not safe, password is saved as clear text)

set remMachine to "eppc://username@192.168.1.10"
(best and safe)

set remMachine to "eppc://192.168.1.10"
(dialog might show username of local user, just inconvinient)
*)


display dialog ¬
	"All windows or just the active window?" buttons {"All", "Active", "Cancel"} ¬
	default button 1

if result = {button returned:"Active"} then
	my get_active_links()
	my open_active_links()
	
else if result = {button returned:"All"} then
	my get_all_links()
	my open_all_links()
	
else if result = {button returned:"Cancel"} then
	-- do nothing	
end if


-- Functions:
-- new_tab, new_window
-- get_active_links, open_active_links
-- get_all_links, open_all_links

on new_tab()
	tell application "Safari"
		tell window 1
			set current tab to (make new tab)
		end tell
	end tell
end new_tab


on new_window()
	tell application "Safari"
		make new document
	end tell
end new_window


on get_active_links()
	tell application "Safari" of machine remMachine
		using terms from application "Safari"
			set numTabs to count tabs of front window
			repeat with x from 1 to numTabs
				set newURL to URL of tab x of front window
				set urlList to urlList & newURL
			end repeat
		end using terms from
	end tell
end get_active_links


on open_active_links()
	tell application "Safari"
		-- open a new window in Safari
		my new_window()
		-- check if window is new and empty
		set emptyWindow to true
		repeat with actualURL in urlList
			if emptyWindow then
				set emptyWindow to false
				-- otherwise create a new tab
			else
				my new_tab()
			end if
			-- open the url
			set URL of document 1 to actualURL
		end repeat
	end tell
end open_active_links


on get_all_links()
	tell application "Safari" of machine remMachine
		using terms from application "Safari"
			try
				set numWindows to count of windows
				repeat with w from 1 to numWindows
					set numTabs to count tabs of window w
					repeat with t from 1 to numTabs
						set newURL to URL of tab t of window w
						set urlList to urlList & newURL
						if t is equal to numTabs then
							if w is less than numWindows then
								set urlList to urlList & "NewWindow"
							end if
						end if
					end repeat
				end repeat
			end try
		end using terms from
	end tell
end get_all_links


on open_all_links()
	-- open windows and tabs on local machine
	tell application "Safari"
		-- open a new window in Safari
		my new_window()
		-- check if window is new and empty
		set emptyWindow to true
		repeat with actualURL in urlList
			set compare1 to actualURL as string
			set compare2 to "NewWindow" as string
			if compare1 is equal to compare2 then
				my new_window()
				set emptyWindow to true
			else
				if emptyWindow then
					set emptyWindow to false
					set URL of document 1 to actualURL
				else
					my new_tab()
					set URL of document 1 to actualURL
				end if
			end if
		end repeat
	end tell
end open_all_links