set raw_plists to name of every file whose name contains \"literatureandlatte\" and name extension is \"plist\"
(*
3/21/09
This script combines what had been 2 separate scripts.
It allows switching of Preferences profiles for Scrivener, via saved plists.
Also allows saving new or modified profiles, or deleting ones no longer needed.
If no named plists are found (in addition to the default Scrivener plist), user is asked if they want to create one.
Has not been tested extensively but appears to perform same as the 2 separate scripts.
*)
property mac_plist_folder : path to preferences
property operating_plist : POSIX path of (path to preferences) & \"com.literatureandlatte.scrivener.plist\"
property operating_plist_name : \"com.literatureandlatte.scrivener.plist\"
property menu_divider : \"- - - - - - - - - - - - - - - - -\"
property menu_item_for_editing : \"Update, add, or delete profiles\"
property overwrite_option : \"Overwrite an existing Preferences profile with most recently used plist\"
property new_option : \"Make a new Preferences profile from current plist\"
property delete_option : \"Delete a Preferences profile no longer needed\"
on run
my check_for_Scrivener()
set plist_info to my get_current_saved_plists()
if plist_info is equal to {{}, {}} then
--if only a default Scrivener profile is found, give user chance to add a named profile
my help_new_user(plist_info)
else
--if named profiles found, ask what user wants to do
set requested_profile to my get_profile_request(plist_info)
if requested_profile is equal to menu_divider or requested_profile is equal to menu_item_for_editing then
--this branch for updating, adding, deleting profiles
set what_to_do to my get_what_to_do()
my execute_what_to_do(what_to_do, plist_info)
else
--otherwise just switch profiles as user has requested
my swap_plists(requested_profile, plist_info)
my start_Scrivener()
end if
end if
end run
on appIsRunning(appname)
tell application \"System Events\"
set processnames to name of every process
end tell
if appname is in processnames then
return true
else
return false
end if
end appIsRunning
on check_for_Scrivener()
if my appIsRunning(\"Scrivener\") then
display dialog \"Please close Scrivener before running this script!\" buttons {\"OK\"}
error number -128 -- nice way to quit not just this subroutine but the script
end if
end check_for_Scrivener
on get_current_saved_plists()
tell application \"Finder\"
tell folder mac_plist_folder
set raw_plists to name of every file whose name contains \"literatureandlatte\" and name extension is \"plist\"
end tell
end tell
copy AppleScript's text item delimiters to old_delimiters
set AppleScript's text item delimiters to \".\"
set saved_plist_nicknames to {}
set saved_plist_full_names to {}
repeat with thisPlist in raw_plists
if number of text items in thisPlist is equal to 5 then
copy fourth text item in thisPlist to end of saved_plist_nicknames
copy thisPlist as string to end of saved_plist_full_names
end if
end repeat
set AppleScript's text item delimiters to old_delimiters
set plist_info to {saved_plist_nicknames, saved_plist_full_names}
return plist_info
end get_current_saved_plists
-- ====== profile switching subroutines start here ===========================
on get_profile_request(plist_info)
set plist_nicknames to item 1 of plist_info
copy menu_divider to end of plist_nicknames
copy menu_item_for_editing to end of plist_nicknames
set profile_selection_list to choose from list plist_nicknames with prompt \"Choose a Scrivener profile to work with\"
if profile_selection_list is equal to false then
error number -128 -- nice way to quit not just this subroutine but the script
end if
set profile_selection to item 1 of profile_selection_list
return profile_selection
end get_profile_request
on swap_plists(name_of_requested_profile, plist_info)
set plist_full_names to item 2 of plist_info
set found to false
repeat with full_name in plist_full_names
if name_of_requested_profile is in full_name then
set profile_name_to_make_operating to full_name as string
set found to true
end if
end repeat
if found is equal to false then
display dialog name_of_requested_profile
display dialog \"Oops - can't find a Scrivener plist to match this name!\"
error number -128 -- nice way to quit not just this subroutine but the script
end if
set switch to false
tell application \"Finder\"
tell folder mac_plist_folder
delete file operating_plist_name
delay 0.3
duplicate file profile_name_to_make_operating to path to home folder
set name of file profile_name_to_make_operating to operating_plist_name
delay 0.3
end tell
set dupe_file to file (((path to home folder) as string) & profile_name_to_make_operating)
move dupe_file to mac_plist_folder
end tell
end swap_plists
on start_Scrivener()
tell application \"Scrivener\"
activate
end tell
end start_Scrivener
-- ====== editing subroutines start here ===========================
on get_what_to_do()
set what_to_do to choose from list {overwrite_option, new_option, delete_option} with prompt \"Choose what to do with your Scrivener Preference profiles\"
if what_to_do is equal to false then
error number -128 -- nice way to quit not just this subroutine but the script
else
return what_to_do
end if
end get_what_to_do
on execute_what_to_do(what_to_do, plist_info)
if what_to_do as string is equal to overwrite_option then
my overwrite_profile(plist_info)
else if what_to_do as string is equal to new_option then
my make_new_profile(plist_info)
else if what_to_do as string is equal to delete_option then
my delete_profile(plist_info)
end if
end execute_what_to_do
on overwrite_profile(plist_info)
set plist_nicknames to item 1 of plist_info
set profile_selection_list to choose from list plist_nicknames with prompt \"Choose a Scrivener Preferences profile to overwrite with the most recently used plist\"
if profile_selection_list is equal to false then
error number -128 -- nice way to quit not just this subroutine but the script
end if
set name_of_requested_profile to item 1 of profile_selection_list
set plist_full_names to item 2 of plist_info
set found to false
repeat with full_name in plist_full_names
if name_of_requested_profile is in full_name then
set profile_name_to_overwrite to full_name as string
set found to true
end if
end repeat
if found is equal to false then
display dialog name_of_requested_profile
display dialog \"Oops - can't find a Scrivener plist to match this name!\"
error number -128 -- nice way to quit not just this subroutine but the script
end if
set switch to false
tell application \"Finder\"
tell folder mac_plist_folder
delete file profile_name_to_overwrite
delay 0.3
duplicate file operating_plist_name to path to home folder
set name of file operating_plist_name to profile_name_to_overwrite
delay 0.3
end tell
set dupe_file to file (((path to home folder) as string) & operating_plist_name)
delay 0.2
move dupe_file to mac_plist_folder
end tell
end overwrite_profile
on make_new_profile(plist_info)
set plist_nicknames to item 1 of plist_info
set nickname_OK to false
repeat while nickname_OK is equal to false
set new_nickname to text returned of (display dialog \"Enter a nickname for the new Preferences profile\" default answer \"\" buttons {\"Cancel\", \"OK\"} default button 2)
if plist_nicknames is equal to {} then
set nickname_OK to true
exit repeat
end if
repeat with full_name in plist_nicknames
if new_nickname is not in full_name then
else
display dialog \"Sorry, that nickname is taken!\"
set nickname_OK to false
exit repeat
end if
set nickname_OK to true
end repeat
end repeat
set new_profile_name to \"com.literatureandlatte.scrivener.\" & new_nickname & \".plist\"
tell application \"Finder\"
tell folder mac_plist_folder
duplicate file operating_plist_name to path to home folder
delay 0.3
set name of file operating_plist_name to new_profile_name
delay 0.2
end tell
set dupe_file to file (((path to home folder) as string) & operating_plist_name)
move dupe_file to mac_plist_folder
end tell
end make_new_profile
on delete_profile(plist_info)
set plist_nicknames to item 1 of plist_info
set profile_selection_list to choose from list plist_nicknames with prompt \"Choose a Scrivener Preferences profile to delete\"
if profile_selection_list is equal to false then
error number -128 -- nice way to quit not just this subroutine but the script
end if
set nickname_of_profile_to_delete to item 1 of profile_selection_list
set plist_full_names to item 2 of plist_info
set found to false
repeat with full_name in plist_full_names
if nickname_of_profile_to_delete is in full_name then
set profile_to_delete to full_name as string
set found to true
end if
end repeat
if found is equal to false then
display dialog name_of_requested_profile
display dialog \"Oops - can't find a Scrivener plist to match this name!\"
error number -128 -- nice way to quit not just this subroutine but the script
end if
set double_check to button returned of (display dialog \"Are you really sure you want to delete Preferences profile '\" & nickname_of_profile_to_delete & \"'? If you do, the associated plist is going away and ain't coming back!\" buttons {\"Cancel\", \"OK\"} default button 1 with icon caution)
tell application \"Finder\"
tell folder mac_plist_folder
delete file profile_to_delete
end tell
end tell
end delete_profile
on help_new_user(plist_info)
set well_gosh_sure to button returned of (display dialog \"Didn't find any plists for Scrivener in ~/Library/Preferences other than the default. If you would like to save the current plist as a new profile, containing the current Preferences for Scrivener, click Yes. Otherwise click No to cancel.\" buttons {\"Yes\", \"No\"} default button 1)
if well_gosh_sure is equal to \"No\" then
error number -128 -- nice way to quit not just this subroutine but the script
else
my make_new_profile(plist_info)
end if
end help_new_user
set raw_plists to {}
tell application \"Finder\"
set folder_list to name of every document file of folder mac_plist_folder
repeat with this_name in folder_list
if this_name contains \"literatureandlatte\" and this_name ends with \".plist\" then
copy this_name to end of raw_plists
end if
end repeat
end tell
It looks like you're new here. If you want to get involved, click one of these buttons!