Hello,
I’m new to AS and had a hard way trying to automate a thing.
It is now working , so i share it:
The problem:
I wanted to modify a default configuration of an application.
The configuration file is inside the package of the app (right click on app & “show package content in Finder”)
But this application is often upgraded, and i was bothered doing it manually, so the script.
Basically it is a search and replace program.
What is tricky (at least to me):
- i did not get at first that when you replace by something shorter, the initial file keeps the extra characters. So I had to reset the initial file with eof before using the AppleScript command “write”
- the file inside the package has a UTF8 encoding and an invisible BOM Header, that “write” does not handle. So I had to add manually the BOM header
- learn to use AppleScript’s text item delimiters with proper class properties (utf8)
I am sure it is not the most elegant script ever writen, but it works
Here it is
set app_path to ":Users:myname:Applications:Delicious Library.app" -- your application path
set terms_list to {¬
{"système décimal dewey", "dewey"}, ¬
{"bibliothèque du Congrès", "Bibl. Congrès"}, ¬
{"profondeur x largeur x hauteur", "p x l x h"}, ¬
{"numéro dans la série", "n° ds la série"}, ¬
{"classement MPAA", "classt MPAA"}} -- the list of terms to change
-- no other configuration needed
set file_name to "humanReadableNamesForMediaKeys.strings" -- file where variables are
set file_path to ":Contents:Resources:fr.lproj:" -- filepath inside delicious library package
set hd to path to desktop
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set hdname to first text item of (hd as string)
set AppleScript's text item delimiters to astid
set file_alias to (hdname & app_path & file_path & file_name) as text -- complete path of file to change
set file_old to alias (file_alias as text)
set AppleScript's text item delimiters to ""
set text_items_of_file_old to text items of (read file_old as «class utf8») -- the right text format
set file_new to text_items_of_file_old as Unicode text
repeat with current_terms in terms_list --let's change all terms
set findw to item 1 of current_terms as string
set subword to item 2 of current_terms as string
set AppleScript's text item delimiters to findw
set text_items_of_file_old to text items of (file_new)
--A list that contains 2 chunks of text: Everything before the original line, and everything after
set AppleScript's text item delimiters to subword
set file_new to text_items_of_file_old as Unicode text --New text for your new file
end repeat
set fRef to (open for access file file_alias with write permission)
try
set eof fRef to 0 -- necessary to erase previous data in file
set myBom to ((ASCII character 239) & (ASCII character 187) & (ASCII character 191))
write myBom to fRef -- "Byte Order Mark" Header needed in the file that write does not handle natively
write file_new to fRef as «class utf8» -- again unix format to be compliant with DL app
end try
close access fRef
set AppleScript's text item delimiters to astid
I put the script in the app script folder, so basically i know where it is when i need it