script to switch File-Sharing on or of.

I wnat to script file sharing but am getting stuck. can anybody point me in the right direction?

it hangs on the radio button line.

thnaks

I Have


do shell script "/usr/bin/open /System/Library/PreferencePanes/SharingPref.prefPane"
activate application "System Preferences"
tell application "System Events"
	tell application process "System Preferences"
		
		click radio button "File Sharing"

	end tell
	end tell

Hi,

this works in Leopard, the UI elements in Tiger are probably different


tell application "System Preferences"
	activate
	reveal anchor "Internet" of pane id "com.apple.preferences.sharing"
end tell

tell application "System Events"
	tell process "System Preferences"
		tell table 1 of scroll area 1 of group 1 of window 1
			tell (1st row whose value of static text 1 is "File Sharing") to click checkbox 1
		end tell
	end tell
end tell
quit application "System Preferences"

yep thanks works a dream in 10.5!

learning all the time.

Is it possible to modify the script that it definitely turns filesharing off or on (not switching). Could anybody help me?
I think i have to modify “click checkbox 1” but i have no idea what to place.

This will set it ON.
If value of checkbox 1 = 1, then it would set OFF

tell application "System Preferences"
	activate
	reveal anchor "Internet" of pane id "com.apple.preferences.sharing"
end tell

tell application "System Events"
	tell process "System Preferences"
		tell table 1 of scroll area 1 of group 1 of window 1
			tell (1st row whose value of static text 1 is "File Sharing")
				if (value of checkbox 1) = 0 then
					click checkbox 1
				end if
			end tell
		end tell
	end tell
end tell
quit application "System Preferences"

sophisticated version, you can call startFileSharing() or stopFileSharing()


on startFileSharing()
	setFileSharing(true)
end startFileSharing

on stopFileSharing()
	setFileSharing(false)
end stopFileSharing

on setFileSharing(bool)
	tell application "System Preferences"
		activate
		reveal anchor "Internet" of pane id "com.apple.preferences.sharing"
	end tell
	
	tell application "System Events"
		tell process "System Preferences"
			tell table 1 of scroll area 1 of group 1 of window 1
				tell (1st row whose value of static text 1 is "File Sharing")
					tell checkbox 1
						if (get value) is ((not bool) as integer) then click
					end tell
				end tell
			end tell
		end tell
	end tell
	quit application "System Preferences"
end setFileSharing

Big thanks for your kind replies.
I’ve seen that the part

is language sensitive. So for my german script i’ve to write “Dateifreigabe” (which is a little bit annoying because i sometimes switch the system language. Is it possible to make this line working in “all” languages?

Yours, tom

@StefanK: ah, i see, so i can start and stop with writing either startFileSharing() or stopFileSharing() in the first line of the script. That’s fine.

Hm,

in my german system (10.5.8) it’s also File Sharing.
Anyway, this script considers localized strings, adjust the property languages,
token is the language identifier, value is the localized string


property languages : {{token:"de", value:"Dateifreigabe"}, ¬
	{token:"en", value:"File Sharing"}, ¬
	{token:"fr", value:"Partage de fichers"}, ¬
	{token:"it", value:"Condivisione documenti"}}

property localizedFileSharingString : "File Sharing"

set systemLanguage to word 1 of (do shell script "defaults read -g AppleLanguages")
repeat with i in languages
	if i's token is systemLanguage then
		set localizedFileSharingString to i's value
		exit repeat
	end if
end repeat

startFileSharing()

on startFileSharing()
	setFileSharing(true)
end startFileSharing

on stopFileSharing()
	setFileSharing(false)
end stopFileSharing

on setFileSharing(bool)
	tell application "System Preferences"
		activate
		reveal anchor "Internet" of pane id "com.apple.preferences.sharing"
	end tell
	
	tell application "System Events"
		tell process "System Preferences"
			tell table 1 of scroll area 1 of group 1 of window 1
				tell (1st row whose value of static text 1 is localizedFileSharingString)
					tell checkbox 1
						if (get value) is ((not bool) as integer) then click
					end tell
				end tell
			end tell
		end tell
	end tell
	quit application "System Preferences"
end setFileSharing

Hm,

gives an error messsage:

error “token of item 1 of every language kann nicht in Typ reference umgewandelt werden.” number -1700 from token of item 1 of every language to reference

but the principle is clear.

there is obviously a terminology clash.
Change all occurrences of languages to theLanguages

Dear Stefan,

you’re genius!

Thanks a lot.

keine Ursache (you’re welcome)

Hi there

I have been trying to adapt this script to turn on file sharing (with FTP sharing turned on under options) and also to tuen on internet sharing.

I have failed, pretty dismally.

Can anyone suggest a way?

Thanks

Andrew

Hi,
because i combined some parts in an automator script to start everything i need for web-development, i use this unter 10.6 (german). It sould be easy to figure out, what to change. If you don’t get it working i’ll try to help tomorrow or on the weekend.

startFileSharing()

on startFileSharing()
setFileSharing(true)
end startFileSharing

on stopFileSharing()
setFileSharing(false)
end stopFileSharing

on setFileSharing(bool)
tell application “System Preferences”
activate
reveal anchor “Internet” of pane id “com.apple.preferences.sharing”
end tell

tell application "System Events"
	tell process "System Preferences"
		tell table 1 of scroll area 1 of group 1 of window 1
			tell (1st row whose value of static text 1 is "Dateifreigabe")
				tell checkbox 1
					if (get value) is ((not bool) as integer) then click
				end tell
			end tell
		end tell
	end tell
end tell
quit application "System Preferences"

end setFileSharing

yours, Tom

Thanks, Tom

I can get file sharing to turn on but the problems surface to achieve the other things:

(1) Turning on FTP file sharing (which is an option under file sharing - which via GUI requires a bit of doing); and
(2) Turning on internet sharing, which is not as simple as turning on file sharing as it throws up a dialog box (a) if airport is turned off and (b) to prompt “Start” sharing. Again, via GUI scripting, I have got stuck.

Cheers

Andrew

Model: MacBook Pro
Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

I have a Déjà Vu

Stefan

Bien vu.

As you can see, I have made no progress since your last intervention!

Have tried this:

do shell script "/bin/launchctl load -w " & "/System/Library/LaunchDaemons/ftp.plist" with administrator privileges
do shell script "/bin/launchctl load -w " & "/System/Library/LaunchDaemons/com.apple.AppleFileServer.plist" with administrator privileges
do shell script "/bin/launchctl load -w " & "/System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges

The first shell script seems to work, but the second two have no effect.

At least the first shell script allows me to get around the FTP issue. Although it did prompt me for the account password. Is there any way around that?

And if you have any further suggestions to shell script the sought after result for internet sharing and file sharing, I am all ears…

Merci beaucoup, as ever. Don’t know where you get to know all this detail:)

You can include username and password in the do shell script line

do shell script "do something" user name "name" password "pass" with administrator privileges

play with delays between the lines, it can take some time to launch the service

Merci, Stefan

Will play around with these and revert.

Andrew

This script simply does not do anything:

do shell script "/bin/launchctl load -w " & "/System/Library/LaunchDaemons/com.apple.AppleFileServer.plist" with administrator privileges

It should turn on file sharing, if I understand matters correctly.

I cannot properly test the internet sharing shell script as I am on WiFi only.

A