I use grep to check if the preference exist, and use sed to remove it if it does exist. But i don’t know how to insert it if it doesn’t exist.
Here’s what I’ve got so far. Could you help me complete it?
on open names
tell application "System Events" to set infoPlist to (application file of names & "Contents:Info.plist")
read from file infoPlist
-- Check if the Presentation mode already exist
set keyexist to text of (do shell script "grep -c LSUIPresentationMode " & (POSIX path of infoPlist))
if keyexist is not 0 then
-- remove the key
set theData to text of (do shell script "sed 's/<key>LSUIPresentationMode<\\/key>//' " & (POSIX path of infoPlist))
set theData to text of (do shell script "sed 's/integer>4<\\/integer>//' " & quoted form of theData)
else
-- insert the key
set thiskey to "<key>LSUIPresentationMode</key>
<integer>4</integer>
"
-- I need help here..
end if
write to file infoPlist
quit
end open
on read from file pathName
-- Open the file so that we can read it in
set theFile to open for access (pathName as POSIX file)
-- Read the data in
set theData to read theFile as string
-- Close the file
close access theFile
end read from file
on write to file pathName
-- Open the file for writing
set theFile to open for access (pathName as POSIX file) with write permission
-- Write the data
write theData to theFile as string
-- Close the file
close access theFile
end write to file
there is a much easier way to do this with the defaults command of the shell
on open names
repeat with i in names
try
((i as Unicode text) & "Contents:Info.plist") as alias -- check if Info.plist exists
set infoPlist to quoted form of POSIX path of ((i as Unicode text) & "Contents:Info")
try
do shell script "defaults read " & infoPlist & " LSUIPresentationMode"
do shell script "defaults delete " & infoPlist & " LSUIPresentationMode"
on error
do shell script "defaults write " & infoPlist & " LSUIPresentationMode 4"
end try
end try
end repeat
end open
grep will throw an error when it doesn’t find anything; You’ll often see it used like this:
-- the rest of the script
try
-- grep shell script
-- grep found a match, do this
on error
-- grep didn't find a match, do something else
end try
-- the rest of the script
However, in this case, instead of doing the work of writing to the file yourself, I would just use the defaults command:
script userDefaults
property _domain : ""
on readKey(_key, _defaultValue)
try
do shell script "/usr/bin/defaults read " & _domain & space & quoted form of _key
on error
return _defaultValue
end try
end readKey
on writeKey(_key, _value)
do shell script "/usr/bin/defaults write " & _domain & space & quoted form of _key & space & quoted form of _value
end writeKey
end script
on run
choose file of type {"APPL"} with prompt "Change `LSUIPresentationMode` for this application: " without invisibles
open {result}
end run
on open theseItems
set thisItem to (first item of theseItems) as Unicode text
tell userDefaults
set it's _domain to quoted form of POSIX path of (thisItem & "Contents:Info") -- `defaults` will add ".plist"
readKey("LSUIPresentationMode", "0")
if result is "0" then
writeKey("LSUIPresentationMode", "4")
else if result is "4" then
writeKey("LSUIPresentationMode", "0")
end if
end tell
end open