Search and Replace
Search and Replace
- Subject: Search and Replace
- From: Simon Topliss <email@hidden>
- Date: Sun, 01 Jul 2001 19:54:27 +0100
We have received over 12GB of files from a customer. The files are made up
of Adobe Illustrator files with placed eps and tif files in them.
Because the Illustrator files were stored in a different location (different
disk, different folder), every time an Illustrator file is opened it asks
for the location of all its linked files.
Using AppleScript I intend to search through each and every Illustrator file
for all its linked files and update the paths to the new locations (if they
exist).
The Illustrator files show their linked files in the following format (the
other code has been cut) if the file has four linked files called
"Bear.tif", "Bear.eps", "Ducky.tif" and "Ducky.eps":
-- ** Begin PostScript Code **
%%DocumentFiles:System:Desktop Folder:Test Folder:Images:Ducky.eps
%%+System:Desktop Folder:Test Folder:Images:Bear.eps
(System:Desktop Folder:Test Folder:Images:Bear.eps)`
%%IncludeFile:System:Desktop Folder:Test Folder:Images:Bear.eps
(System:Desktop Folder:Test Folder:Images:Bear.tif) 0 XG
(System:Desktop Folder:Test Folder:Images:Ducky.eps)`
%%IncludeFile:System:Desktop Folder:Test Folder:Images:Ducky.eps
(System:Desktop Folder:Test Folder:Images:Ducky.tif) 0 XG
-- ** End PostScript Code **
I think that BBEdit will be the fastest way to search and replace the file
paths (please correct me if I'm wrong) and have got as far as the following
(watch for text wraps):
-- ** Script Start **
set searchStrings to {".tif", ".eps", ".jpg"}
set searchFolder to "System:Desktop Folder:Test Folder:" -- change to parent
folder to search
set searchedItems to {} -- no point in searching folder for something twice
set oldDelims to AppleScript's text item delimiters
-- create text file for results of searches
set textFilePath to (path to desktop folder as text) & "Search Results.txt"
try
set textFileRef to open for access file textFilePath with write
permission
on error
close access file textFilePath
set textFileRef to open for access file textFilePath with write
permission
end try
-- repeat with every file type
repeat with aSearchString in searchStrings
set searchForIt to false
tell application "BBEdit 6.1"
if windows is not {} then -- check for a open file
set searchOptions to {starting at top:true, returning
results:true}
set searchResults to (find aSearchString searching in {text
window 1} options searchOptions)
if found of searchResults is true then
repeat with foundItem in found matches of searchResults
set AppleScript's text item delimiters to ":"
set filename to last text item of message of foundItem
set AppleScript's text item delimiters to aSearchString
if filename does not end with aSearchString then
set filename to first text item of filename &
aSearchString
end if
if searchedItems is not {} then
set AppleScript's text item delimiters to ":"
repeat with anItem in searchedItems
if last text item of anItem = filename then
set fileSearch to {anItem}
set searchForIt to false
exit repeat
else
set searchForIt to true
end if
end repeat
else
set searchForIt to true
end if
if searchForIt is true then
set fileSearch to FindFile in_folder searchFolder
name_is filename with subfolders -- requires FindFile osax
if fileSearch is not {} then
set foundResult to item 1 of fileSearch
set end of searchedItems to foundResult
write filename & tab & "path resolved" & return
to textFileRef
else
write filename & tab & "not found" & return to
textFileRef
end if
end if
set textLine to line (result_line of foundItem) of
window 1
set textLineText to text of textLine
set AppleScript's text item delimiters to aSearchString
-- Not worth checking if the file path doesn't need
fixing
-- all file paths will be wrong
if text of textLine starts with "%%DocumentFiles:" then
set text of textLine to "%%DocumentFiles:" & item 1
of fileSearch
else if text of textLine starts with "%%+" then
set text of textLine to "%%+" & item 1 of fileSearch
else if textLineText starts with "(" then
set text of textLine to "(" & item 1 of fileSearch &
text item -1 of (textLineText as text)
else if text of textLine starts with "%%IncludeFile:"
then
set text of textLine to "%%IncludeFile:" & item 1 of
fileSearch
end if
end repeat
end if
end if
end tell
end repeat
close access file textFilePath
set AppleScript's text item delimiters to oldDelims
-- ** Script End **
As the number of files I will be checking is in the thousands and each file
is between 1MB and 50MB any suggestions for speed gains/optimisation I can
achieve by improving the above script will be greatly appreciated.
I read BBEdit's documentation on grep searches and have a feeling that they
could achieve what I'm doing more efficiently, but had trouble understanding
the syntax. Can anyone help?
Simon