Wednesday, August 10, 2022

#1 2011-09-16 11:07:17 am

eponymic
Member
Registered: 2011-09-16
Posts: 3

.plist sync trouble/first script

Hi all, I am having issues with my attempt to sync plists across computers on a LAN, my basic approach has been to set up my plists the way I want them, then store them in the application bundle, then overwrite the existing plist from the app bundle.

The plists I am worried about are, dock, safari, and mail, I understand mail and safari are folders.

So, I want to write to the user library from the app bundle, overwriting the default folders/plists, then I want to restart the computer.

What I have so far.

Applescript:

display dialog "This Script requires that you restart your computer after running, you must run this as Admin or higher" buttons {"Cancel", "OK"} default button 2 --If Cancel if result = {button returned:"Cancel"} then
display dialog "Please attempt later"

--If OK
else if result = {button returned:"Yes"} then
display dialog "I can't believe you would just willy nilly let some script a guy you don't know wrote restart your computer, do you even know what this script does?"
end if
--main


--overwrite preferences and safari preferences with files from application bundle --set path and directory references
set myUserLib to path to library folder as string -- the library folder of the current user try
set mySafariPrefs to ((path to me as Unicode text) & "Contents:Resources:Safari") as alias
tell application "Finder" to duplicate mySafariPrefs to myUserLib replacing yes
on error
display dialog "Preference file couldn't be found, make sure path of plist is Contents>Resources" buttons {"Cancel"} default button 1
end try
try


set myUserPrefs to ((path to me as Unicode text) & "Contents:Resources:Prefrences") as alias
tell application "Finder" to duplicate myUserPrefs to myUserLib replacing yes
on error
display dialog "Preference file couldn't be found, make sure path of plist is Contents>Resources" buttons {"Cancel"} default button 1
end try
--end set path and directory references
--end overwrite
--end main
tell application "Finder"
restart
end tell

And here is where I run into a brick wall, I can't seem to figure out how to make all the pieces fit together.

Sorry for the newb question, first script.


Filed under: safari, mail, dock, plist

Offline

 

#2 2011-09-16 08:40:00 pm

hubionmac
Member
From:: Germany
Registered: 2009-08-30
Posts: 28
Website

Re: .plist sync trouble/first script

Well... I got your point and I think the idea of saving an Apps preferences (Application Support, Preferences, Library Folder) got me...
This Script should be saved as an an or as a script-bundle, it asks you for an action (restore or backup)

On Backup

you may choose an app and the script searches for it's preferences (based on the entries in the app's info.plist) in the user's library and stores everything in a compressed tar archive, located in the script's resources folder.

On Restore

the script just unpacks a selected tar archive to the user's library, restoreing the saved Preferences...
big_smile

Applescript:

--17.09.2011 hubionmac.com
-- Prefs-Save-Restore_v1: Saves and Restores an app's preferences, Library Folder, Application Support Folder using a tar archive
-- inspired by [url]http://macscripter.net/viewtopic.php?id=37052[/url]

-- STORE THIS AS AN APPLICATION OR SCRIPT BUNDLE!!!!
global store_here
--first get the folder where all the saved prefs should be saved….
try
   set store_here to quoted form of POSIX path of (path to resource "saved_prefs")
on error msg
   do shell script "mkdir " & quoted form of POSIX path of (path to me as alias) & "Contents/Resources/saved_prefs"
   --NOT USING PATH TO RESSOURCE BECAUSE IT DOESN'T SEEM TO UPDATE THAT FAST….
   set store_here to quoted form of POSIX path of (path to me as alias) & "Contents/Resources/saved_prefs"
end try
-- WHAT TO DO?
set theaction to button returned of (display dialog "Do you want to restore or backup an app Prefs?" buttons {"Restore", "Backup", "Cancel"} default button {"Restore"})
if theaction = "Backup" then
   make_backup(choose file of type "app")
else if theaction = "Restore" then
   make_restore()
end if

on make_restore()
   set thebackups to every paragraph of (do shell script "ls " & store_here)
   if thebackups = {} then
       error "There are no backups that can be used to restore…."
   else
       set t to (choose from list thebackups with prompt "Choose one or more backup(s):" default items (item 1 of thebackups) with multiple selections allowed)
       if t ≠ false then
           repeat with m in t
               do shell script "cd ~/Library; tar xf " & store_here & quoted form of m
               my display_message("Restored " & m & ".", 2)
           end repeat
       end if
   end if
   
end make_restore
on make_backup(this)
   set search_folders to {"./", "'Application Support'/", "Preferences/"}
   tell application "Finder"
       --GET THE IMPORTANT INFOS OUT OF INFO.PLIST FOR GETTING THE NAMES OF PLIST AND OTHER FILES AND FOLDERS INSIDE THE LIBARAY
       set this_info_plist to quoted form of POSIX path of (item "Info.plist" of folder "Contents" of this as alias)
       set this_id to do shell script "defaults read " & this_info_plist & " CFBundleIdentifier"
       set this_bundleName to do shell script "defaults read " & this_info_plist & " CFBundleName"
       --THIS LOOKS COMPLICATED BUT IS FASTER THAN USING THE FINDER
       --AND IS IS THAT COMPLICATED BECAUSE OF THE OUTPUT OF THE FIND COMMAND THAT NEEDS TO BE QUOTED ;-/
       set this_app_files to {}
       repeat with search_folder in search_folders
           --SEARCH THE SEARCH_FOLDER FOR FILES AND FOLDERS BUT ONLY ON THE FIRST LEVEL!!!
           set tmp to every paragraph of (do shell script "cd ~/Library/" & search_folder & "; find . -maxdepth 1 -name '" & this_id & "*' -exec basename {} \\;")
           repeat with t in tmp
               set this_app_files to this_app_files & ((search_folder & quoted form of t) as text)
           end repeat
           set tmp to every paragraph of (do shell script "cd ~/Library/" & search_folder & "; find . -maxdepth 1 -name '" & this_bundleName & "' -exec basename {} \\;")
           repeat with t in tmp
               set this_app_files to this_app_files & ((search_folder & quoted form of t) as text)
           end repeat
       end repeat
       set old_delimiters to AppleScript's text item delimiters
       set AppleScript's text item delimiters to " "
       set this_app_files to this_app_files as text
       set AppleScript's text item delimiters to old_delimiters
       --NOW MAKE A TAR FILE OF ALL THESE IN THIS APPS RESSOURCE FOLDER
       do shell script "cd ~/Library/; tar -czf " & store_here & this_id & ".tar.gz " & this_app_files
       my display_message("Stored " & this_id & ".", 2)
   end tell
end make_backup


on display_message(msgTXT, msgTimeout)
   
   tell application "System Events"
       set isRunning to ¬
           (count of (every process whose name is "GrowlHelperApp")) > 0
   end tell
   if isRunning = true then
       tell application "GrowlHelperApp"
           -- Make a list of all the notification types
           -- that this script will ever send:
           set the allNotificationsList to ¬
               {"Status"}
           
           -- Make a list of the notifications
           -- that will be enabled by default.
           -- Those not enabled by default can be enabled later
           -- in the 'Applications' tab of the growl prefpane.
           set the enabledNotificationsList to ¬
               {"Status"}
           
           -- Register our script with growl.
           -- You can optionally (as here) set a default icon
           -- for this script's notifications.
           register as application ¬
               "Finder" all notifications allNotificationsList ¬
               default notifications enabledNotificationsList ¬
               icon of application "Finder"
           
           
           --    Send a Notification...
           notify with name ¬
               "Status" title ¬
               "Prefs-Safe-Restore" description ¬
               msgTXT application name ¬
               "Finder"
           return true
       end tell
   else
       activate
       display dialog msgTXT giving up after msgTimeout
   end if
end display_message

Offline

 

#3 2011-09-19 01:28:52 pm

eponymic
Member
Registered: 2011-09-16
Posts: 3

Re: .plist sync trouble/first script

Couple questions

Applescript:


--17.09.2011 hubionmac.com
-- Prefs-Save-Restore_v1: Saves and Restores an app's preferences, Library Folder, Application Support Folder using a tar archive
-- inspired by [url]http://macscripter.net/viewtopic.php?id=37052[/url]

-- STORE THIS AS AN APPLICATION OR SCRIPT BUNDLE!!!!
global store_here
--first get the folder where all the saved prefs should be saved….
try
set store_here to quoted form of POSIX path of (path to resource "saved_prefs")
on error msg
do shell script "mkdir " & quoted form of POSIX path of (path to me as alias) & "Contents/Resources/saved_prefs"
--NOT USING PATH TO RESSOURCE BECAUSE IT DOESN'T SEEM TO UPDATE THAT FAST….
set store_here to quoted form of POSIX path of (path to me as alias) & "Contents/Resources/saved_prefs"
end try
-- WHAT TO DO?
set theaction to button returned of (display dialog "Do you want to restore or backup an app Prefs?" buttons {"Restore", "Backup", "Cancel"} default button {"Restore"})
if theaction = "Backup" then
make_backup(choose file of type "app")
else if theaction = "Restore" then
make_restore()
end if

From my understanding, this section contains the user interface, as well, it also sets the location of the app preferences to be restored, correct?

Applescript:


on make_restore()
set thebackups to every paragraph of (do shell script "ls " & store_here)
if thebackups = {} then
error "There are no backups that can be used to restore…."
else
set t to (choose from list thebackups with prompt "Choose one or more backup(s):" default items (item 1 of thebackups) with multiple selections allowed)
if t ≠ false then
repeat with m in t
do shell script "cd ~/Library; tar xf " & store_here & quoted form of m
my display_message("Restored " & m & ".", 2)
end repeat
end if
end if
end make_restore

This part copies the user back up into the user library?


Applescript:



on make_backup(this)
set search_folders to {"./", "'Application Support'/", "Preferences/"}
tell application "Finder"
--GET THE IMPORTANT INFOS OUT OF INFO.PLIST FOR GETTING THE NAMES OF PLIST AND OTHER FILES AND FOLDERS INSIDE THE LIBARAY
set this_info_plist to quoted form of POSIX path of (item "Info.plist" of folder "Contents" of this as alias)
set this_id to do shell script "defaults read " & this_info_plist & " CFBundleIdentifier"
set this_bundleName to do shell script "defaults read " & this_info_plist & " CFBundleName"
--THIS LOOKS COMPLICATED BUT IS FASTER THAN USING THE FINDER
--AND IS IS THAT COMPLICATED BECAUSE OF THE OUTPUT OF THE FIND COMMAND THAT NEEDS TO BE QUOTED ;-/
set this_app_files to {}
repeat with search_folder in search_folders
--SEARCH THE SEARCH_FOLDER FOR FILES AND FOLDERS BUT ONLY ON THE FIRST LEVEL!!!
set tmp to every paragraph of (do shell script "cd ~/Library/" & search_folder & "; find . -maxdepth 1 -name '" & this_id & "*' -exec basename {} \\;")
repeat with t in tmp
set this_app_files to this_app_files & ((search_folder & quoted form of t) as text)
end repeat
set tmp to every paragraph of (do shell script "cd ~/Library/" & search_folder & "; find . -maxdepth 1 -name '" & this_bundleName & "' -exec basename {} \\;")
repeat with t in tmp
set this_app_files to this_app_files & ((search_folder & quoted form of t) as text)
end repeat
end repeat
set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set this_app_files to this_app_files as text
set AppleScript's text item delimiters to old_delimiters
--NOW MAKE A TAR FILE OF ALL THESE IN THIS APPS RESSOURCE FOLDER
do shell script "cd ~/Library/; tar -czf " & store_here & this_id & ".tar.gz " & this_app_files
my display_message("Stored " & this_id & ".", 2)
end tell
end make_backup

This part of the script seems to be non functional,
Error Message

2011-09-19 14:57:19:892 defaults[16371:60f] The domain/default pair of (/Applications/Safari.app/Contents/Into.plist, CFBundleIdentifier) does not exist


With the options, Edit, and, OK

When I open safaris info.plist I find the string <key>CFBundleIdentifier</key>

Anyone understand how this functions and how to make it work?




[applescript]
on display_message(msgTXT, msgTimeout)
   
   tell application "System Events"
       set isRunning to ¬
           (count of (every process whose name is "GrowlHelperApp")) > 0
   end tell
   if isRunning = true then
       tell application "GrowlHelperApp"
           -- Make a list of all the notification types
           -- that this script will ever send:
           set the allNotificationsList to ¬
               {"Status"}
           
           -- Make a list of the notifications
           -- that will be enabled by default.
           -- Those not enabled by default can be enabled later
           -- in the 'Applications' tab of the growl prefpane.
           set the enabledNotificationsList to ¬
               {"Status"}
           
           -- Register our script with growl.
           -- You can optionally (as here) set a default icon
           -- for this script's notifications.
           register as application ¬
               "Finder" all notifications allNotificationsList ¬
               default notifications enabledNotificationsList ¬
               icon of application "Finder"
           
           
           --    Send a Notification...
           notify with name ¬
               "Status" title ¬
               "Prefs-Safe-Restore" description ¬
               msgTXT application name ¬
               "Finder"
           return true
       end tell
   else
       activate
       display dialog msgTXT giving up after msgTimeout
   end if
end display_message[/applescript

This section has semantic errors when I attempt to run it.

Offline

 

#4 2011-09-20 03:02:45 pm

hubionmac
Member
From:: Germany
Registered: 2009-08-30
Posts: 28
Website

Re: .plist sync trouble/first script

To make it short... you are probably right about the function of each part of the script.
Because of the "CFBundleIdentifier-Error"
In your error Message I see a t instead of the f (Into.plist) is this just an error in your post or in the copy of the script you run? Maybe you just try to run the defaults command on that file manually in your terminal and see if you got the same error.

The last section seems to have errors, when don't have growl installed... well, if you don't use growl, just remove that sections and every call of that function....

Offline

 

#5 2011-09-21 10:20:51 am

eponymic
Member
Registered: 2011-09-16
Posts: 3

Re: .plist sync trouble/first script

Because of the "CFBundleIdentifier-Error"
In your error Message I see a t instead of the f (Into.plist) is this just an error in your post or in the copy of the script you run? Maybe you just try to run the defaults command on that file manually in your terminal and see if you got the same error.


That is an error in my transcription of the error message, not in the actual script.

Same error message in terminal, Do I need to have admin rights to run this?

Offline

 

#6 2011-09-23 11:47:00 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 611

Re: .plist sync trouble/first script

eponymic wrote:

2011-09-19 14:57:19:892 defaults[16371:60f] The domain/default pair of (/Applications/Safari.app/Contents/Into.plist, CFBundleIdentifier) does not exist


Manpage for defaults wrote:

filepath  Domains may also be specified as a path to an arbitrary plist file, omitting the '.plist' extension.


This works:

Applescript:

set thisApp to POSIX path of (choose file of type "app" with prompt "Select the application:")
set thisApp_info_plist to thisApp & "Contents/Info"
set thisApp_id to do shell script "defaults read " & thisApp_info_plist & " CFBundleIdentifier"

@hubionmac: did your script work on your mac?
It would also be wise to check if an app is running, and tell it to quit before restoring its prefs.

Last edited by alastor933 (2011-09-23 11:56:21 am)

Offline

 

#7 2011-09-27 11:34:43 am

hubionmac
Member
From:: Germany
Registered: 2009-08-30
Posts: 28
Website

Re: .plist sync trouble/first script

Yep, the Script worked as a charm on my machine (10.7.1)... would be mean to post code that does not work for myself ;-P
Well, looks like you found a solution after all...

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)