Can't figure out how to read from plist

Hi All, I have a script to check if plist exists, if not it writes one. Then displays dialog and gets user input and writes username and email address to said plist value as string. That script rus this script that checks my IP, and if it's changed it sends an email. I had this working, but had the name and address written into the script. I am now trying to go further and pull those properties to the email fields from the plist.but I get an error saying "Can’t make "username" into type integer." Can some one please help me with this.


set the plistfile_path to path to desktop folder as string --(Seperate problem, I also can't figure out how to set path to "/Users/<user>/Library/Preferences")

set p_list to POSIX path of (plistfile_path & "example.plist")

set theName to value of item "username" of p_list --This line and the next line is where I'm having problems

set theAddress to value of item "emailaddress" of p_list


try


--See what the Current IpAddress is

set Current_ipAddress to do shell script "/usr/bin/curl http://checkip.dyndns.org | /usr/bin/awk '/: / {print $6}' | /usr/bin/cut -f 1 -d '<' "


--set Current_ipAddress to "192.168.1.256" as text --FOR TESTING PURPOSE ONLY


set ipAddress to Current_ipAddress



--See what the Old IPAddress is in the "ScriptCheckIPAddress.Plist file"


-- ScriptCheckIPAddress is the name of the variable


-- ipAddress is the value as text

set Old_IPAddress to (do shell script "defaults read ScriptCheckIPAddress ipAddress")


if Old_IPAddress = Current_ipAddress then


-- Don't do anything because nothing changed.

else


do shell script ("defaults write ScriptCheckIPAddress ipAddress " & ipAddress as text)



--If IPAddress is changed then send an Email

tell application "Mail"

set newMessage to make new outgoing message with properties {visible:true, subject:"Your IP Address has changed", content:"Your Current IPAddress is: " & Current_ipAddress}

tell newMessage


makenewto recipientat end of to recipientswith properties {name:theName, address:theAddress} --This is where I'm trying to use them

end tell


sendnewMessage

end tell

end if

on error --> no such file yet so write it to plist file

do shell script ("defaults write ScriptCheckIPAddress ipAddress " & ipAddress as text)


end try

iMac, Mac OS X (10.7.3), new 21" I5 Imac

Posted on Mar 23, 2012 5:21 PM

Reply
4 replies

Mar 23, 2012 7:29 PM in response to adown

Standard Additions has a path to command that will return paths to most of the common places.


You will need to use either the Property List Suite from System Events or the defaults shell utility to access the individual keys in the plist file. Since you are using defaults in your example, that is what I used in mine. The following example script includes plist read, write, file check and creation:


set prefsFolder to (path to preferences from user domain) as text -- the user's preferences folder
set plistFile to POSIX path of (prefsFolder & "example") -- the plist file, minus extension
#default values
set username to "Joseph Blow"
set emailaddress to "nunya@bizness"
set ipaddress to "192.168.1.255"
try -- test for file and read values if there
(plistFile & ".plist") as POSIX file as alias
set username to (do shell script "defaults read " & plistFile & " username")
set emailaddress to (do shell script "defaults read " & plistFile & " emailaddress")
on error errmess -- no file, so make one
log errmess
do shell script "touch " & quoted form of (plistFile & ".plist") -- make new file
do shell script "defaults write " & plistFile & " username " & quoted form of username
do shell script "defaults write " & plistFile & " emailaddress " & emailaddress
do shell script "defaults write " & plistFile & " ipaddress " & ipaddress
end try
set currentAddress to word 25 of (do shell script "curl checkip.dyndns.org")
set oldAddress to (do shell script "defaults read " & plistFile & " ipaddress")
if oldAddress is not currentAddress then -- update and notify
do shell script ("defaults write " & plistFile & " ipaddress " & currentAddress)
tell application "Mail"
activate
set newMessage to make new outgoing message with properties {visible:true, subject:"Your IP address has changed", content:"Your current IP address is: " & currentAddress}
tell newMessage
make new to recipient at end of to recipients with properties {name:username, address:emailaddress}
end tell
-- send newMessage -- remove comment to 'go live'
end tell
else
log "no change"
end if

Mar 24, 2012 4:43 AM in response to red_menace

Hi, thanks for taking time to reply, and I'm sorry for confusion of the two types of script. The only reason I am using shell script to test IP is I haven't figured out how to test it with Applescript. I will use only Applescript once I get it all figured out, as I undeerstand shell script even less than I understand Applescript.


The begining script writes user name and email address to a plist from user input, then runs the above script to test and send mail. I just can't figure out how to have mail "make new message" with the "username" and "emailaddress" values already contained in the plist. I have seen that I can't use "property list file" unless it is directly under "tell application system events", but I am stuck on how to do this, and have mail use the values. Thanks again


tell application "Mail"

set theName to value of property list file item "username" of p_list --this is what I've been trying to figure out.

set theAddress to value of property list item "emailAddress" of p_list --p_list path is at the begining of script.

set newMessage to make new outgoing message with properties{visible:true, subject:"Your IP Address has changed",content:"Your Current IPAddress is: " & Current_ipAddress}

tell newMessage


makenewto recipientat end of to recipientswith properties {name:theName,address:theAddress} --This is where I'm trying to use them

end tell

Mar 24, 2012 7:35 AM in response to adown

AppleScript doesn't do a lot of stuff by itself, such as getting IP addresses, but instead calls on other applications or utilities to do things beyond the language itself. A shell script is a typical way to get the IP address, I just used a shorter one.


My example checks to see if the plist file exists (the try...on error...end try part) - if it does the username and emailaddress keys are read from the plist, otherwise a new plist file is created and set to the default values. You didn't include your other script, so I just posted a complete script - the plist file should have a format such as:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>emailaddress</key>
<string>nunya@bizness</string>
<key>ipaddress</key>
<string>192.168.1.255</string>
<key>username</key>
<string>Joseph Blow</string>
</dict>
</plist>


Removing the plist file test/create part, the script would be something like the following (the plist file needs to exist in the user's preferences folder, so change the name as needed):

set prefsFolder to (path to preferences from user domain) as text -- the user's preferences folder
set plistFile to POSIX path of (prefsFolder & "example") -- the plist file, minus extension
set username to (do shell script "defaults read " & plistFile & " username")
set emailaddress to (do shell script "defaults read " & plistFile & " emailaddress")
set currentAddress to word 25 of (do shell script "curl checkip.dyndns.org")
set oldAddress to (do shell script "defaults read " & plistFile & " ipaddress")
if oldAddress is not currentAddress then -- update and notify
do shell script ("defaults write " & plistFile & " ipaddress " & currentAddress)
tell application "Mail"
activate
set newMessage to make new outgoing message with properties {visible:true, subject:"Your IP address has changed", content:"Your current IP address is: " & currentAddress}
tell newMessage
make new to recipient at end of to recipients with properties {name:username, address:emailaddress}
end tell
-- send newMessage -- remove comment to 'go live'
end tell
else
log "no change"
end if

Mar 24, 2012 4:34 PM in response to adown

Red_Menace, Thank you so much. I was reading from my cell phone, and couldn't respond. I could click the "solved my Question" button. I want to thank you for taking the time to help a newb. I am having fun learning Applescript, but it is like learning a foreign language. It helps to have someone to turn to. Thanks again, adown

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can't figure out how to read from plist

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.