tell application "System Events"
set item1 to (value of property list item "item1" of contents of property list file "~/Library/Preferences/com.test.plist"
end tell
I am trying to read the value which is in “1, 2, 3, 4” format (bounds of a finder window) stored in the plist without quotes.
When the value is read it utilizes the quotes as well and I would like to be able to use the value without them.
Basically I need the variable to represent: {1, 2, 3, 4}
I don’t understand your question completely, but if you want to convert a string of four numbers separated by commas to a list, it can be done quite easily with the AppleScript’s text item delimiters:
set str to "1, 2, 3, 4"
set delim to "," & space as string
stringToList(str, delim, true)
on stringToList(theString, delim, coerce)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set allItems to every text item of theString
set AppleScript's text item delimiters to tid
-- false returns list of strings
-- true returns list of floats
-- true takes more time
if coerce is true then
set newList to {}
repeat with i in allItems
set end of newList to (i as real)
end repeat
set allItems to newList
end if
return allItems
end stringToList
THANKS! Much obliged everyone! Was driving me mad.
I will also ask while everyone is here, is there any way to build a list of all the open application windows or a list of all the apps that are currently open?
Now I need to save a list in my preference file (plist) and read it into a script with each item quoted, so. . .
set AppleScript's text item delimiters to ", "
set appList to {"item1", "item2", "item3"}
do shell script "defaults write com.test.example 'applist' " & "'" & appList & "'"
This stores the data in the .plist like this: item1, item2, item3 (with no quotes around any item)
and I need it to be stored like this: “item1”, “item2”, “item3” (with quotes on each item)
so I can later bring this data back into another script and have it understand it properly
How about using properties?
They are saved when the script is done running. When you recompile the script, you loose the data, so don’t recompile it unless you are changing code.
property var : {}
if var is {} then
set var to {"item1", "item2", "item3"}
else
return "Already set."
end if
In this case:
property applist : {}
if applist is {} then
tell application "System Events" to set applist to name of every process whose background only is false
else
tell application "System Events"
if (name of every process whose background only is false) is applist then
return true
else
return false
end if
end tell
end if
Or write to a file as a list and read it back as a list. It returns is as a list and nothing changed.
property DOUBLE_QUOTE : "\""
set str to listToString({"item1", "item2", "item3"})
on listToString(l)
set myString to {}
repeat with i from 1 to (count l)
set end of myString to DOUBLE_QUOTE
set end of myString to (item i of l) as string
set end of myString to DOUBLE_QUOTE
if i is not (count l) then set end of myString to ", "
end repeat
return myString as string
end listToString