Getting a value from a Plist file.

Hello,

I am new here and in scripting in applescript. I searched this fine website for informations but I didn’t find anything.

I am trying to get a specific value from a plist file, the askForPassword value in com.apple.screensaver.plist to be exact.

How do I get that?

Respectfully,
Akatash

alternatively try this:

set text item delimiters to {""}
set MACaddress to words 2 thru 7 of (do shell script "ifconfig en0 ether | grep -i ether") as Unicode text
set askForPassword to do shell script "defaults read ~/Library/Preferences/ByHost/com.apple.screensaver." & MACaddress & " askForPassword"

What I want to do is a script that takes the password off if the password is set, for the screensaver, and vice versa.
At least that’s the hole basic structure.

So I did this :


-- This code is thanks to StefanK
set text item delimiters to {""}
set MACaddress to words 2 thru 7 of (do shell script "ifconfig en0 ether | grep -i ether") as Unicode text
set askForPassword to do shell script "defaults read ~/Library/Preferences/ByHost/com.apple.screensaver." & MACaddress & " askForPassword"

-- My code
if (MACaddress = 0) then
	do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 1"
else
	do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 0"
end if

I run this and it doesn’t do anything :slight_smile:

the question is: where is the com.apple.screensaver.plist file.

I don’t have one in ~/Library/Preferences/ but in ~/Library/Preferences/ByHost/ which includes the Ethernet MACaddress
of the computer like com.apple.screensaver.112233445566.plist
If you have only the file in ~/Library/Preferences/ then this should work

-- This code is thanks to StefanK
set askForPassword to do shell script "defaults read com.apple.screensaver askForPassword"

-- My code
if (askForPassword = 0) then
	do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 1"
else
	do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 0"
end if

Well, the file is, infact, in /Library/Preferences/ByHost/
But the file name is com.apple.screensaver.0019e334c983.plist

So I have this


set askForPassword to do shell script "defaults read ~/Library/Preferences/ByHost/com.apple.screensaver.0019e334c983 askForPassword"

if (askForPassword = 0) then
	do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 1"
	do shell script "/usr/local/bin/notif" 
	
else
	do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 0"
	do shell script "/usr/local/bin/notif"
end if

It compiles but it doesnt do anything. I’m kinda getting frustration on this stuff. I think this would be a basic thing to do :stuck_out_tongue:

ok, then your code cannot work, because you’re addessing the file in the Preferences folder
This should do it:

set text item delimiters to {""}
set MACaddress to words 2 thru 7 of (do shell script "ifconfig en0 ether | grep -i ether") as Unicode text
set screensaverPlist to "~/Library/Preferences/ByHost/com.apple.screensaver." & MACaddress
set askForPassword to do shell script "defaults read " & screensaverPlist & " askForPassword"

-- My code
if (askForPassword = 0) then
	do shell script "defaults write " & screensaverPlist & " askForPassword -int 1"
else
	do shell script "defaults write " & screensaverPlist & " askForPassword -int 0"
end if

I’ve tested but it still doesnt work :frowning:

Basically I’ve noticed that the code goes directly to the else statement, regardless the number that is on the condition.
That is very odd. So maybe the problem is in getting the value of the askForPassword var.

Sorry for all the work :frowning:

You’re right. Change this line:

set askForPassword to (do shell script "defaults read " & screensaverPlist & " askForPassword") as integer

It works! Thanks StefanK! I owe you big time!