Hello Russ,
Thank you for valuable feedback. You're right that the logging should be refined. I chose the simplest fix to put logging statement before the conversion. The fix is minimum but for convenience, I repost the entire code of revised version of three scripts – save_AW6_WP_as_Pages4.applescript, save_AW6_SS_as_Numbers2.applescript and save_AW_as_v6.applescript.
As for the error -609 connectionInvalid, it is a sign AppleScript runtime received when target application, in this case Numbers.app, has died. It is Numbers.app's problem that I have no control. Although it is possible to terminate the script more gracefully when this sort of error raised, I'm too lazy to clean up the mess caused by a poorly written application and thus let it die as it does. 😝
By the way,
... AW6 always wanted to save any I opened as Appleworks (not Appleworks 5.0 or ClarisWorks 4.0). ...
might indicate that those AW files were of older version. If it were already AW6 file, AW6 would not present save dialogue when you're closing the file without edit. Only when it is pre-AW6 file, AW6 asks to save the opened document even without edit because the opened document is indeed a new document converted in memory. So I'd rather think your semi-manual conversions of AW/CW to AW6 had worked as expected and not at all in vain.
FWIW, there's more direct way to determine the old version files, that is to check the following option in AW6 preferences setting and open the file in question which should present an alert dialogue box for old version file.
AppleWorks > Preferences… > General > Files > Open Documents > Old Version Alert
I assumed that this option is NOT checked and indeed it should NOT be checked for the save_AW_as_v6.applescript to work correctly.
Any way, all's well that ends well.
Best wishes from Japan,
Hiroto
--save_AW6_WP_as_Pages4.applescript
(*
Convert AppleWorks v6 WP (word processing) files to iWork'09's Pages v4 files
v0.2
v0.2 -
changed the logging scheme from post-conversion log:
converted x => y
to pre-conversion log:
converting x => y
so that it can help to identify the last file which had caused the applicaton / script to crash.
v0.1 -
initial version
* Tested with Pages 4.0.5 under OS X 10.6.8.
* Specify property in_place in script as necessary.
* Specify property inherit_dates in script as necessary.
* A log file with time-stamp in name is created on desktop.
* Original file name is always preserved except that name extension ".pages" is added
Note that original name extension (.cwk) is NOT removed and thus, e.g., abc.cwk will be converted to abc.cwk.pages
This is to avoid possible data loss in case there're file names, e.g., abc and abc.cwk in a source directory.
*)
_main()
on _main()
script o
property in_place : true
(*
true : convert files in place, i.e. converted files are saved in original directories,
where original source directory is archived in zip file with time-stamp in name in advance;
false : converted files are saved under destination directory which is specified in a dialogue in script
*)
property inherit_dates : true
(*
true : destination file inherits creation date and modification date of source file
false : destination file has creation date and modification date at the time of conversion
*)
property ext : ".pages"
property logfile : (path to desktop)'s POSIX path & (do shell script "date +'save_AW6_WP_as_Pages4.log_%Y%m%dT%H%M%S.txt'")
property pp : {}
-- accept source directory (and destination direcotry if in_place is false)
set srcd to (choose folder with prompt "Choose source directory where source files reside.")'s POSIX path
if srcd ends with "/" and (count srcd) > 1 then set srcd to srcd's text 1 thru -2
if not in_place then
repeat
set dstd to (choose folder with prompt "Choose destination directory where to save converted files.")'s POSIX path
if dstd ends with "/" and (count dstd) > 1 then set dstd to dstd's text 1 thru -2
if srcd ≠ dstd then exit repeat
display dialog "Source and destination directories cannot be the same!" with icon stop
end repeat
end if
log_printf(logfile, "conversion started in operation mode: in_place = %s, inherit_dates = %s.\\n", {in_place, inherit_dates})
-- retrieve target files
log_printf(logfile, "scanning files under %s\\n", srcd)
set pp to _match(my is_AW_v6_WP, scan_AW_files(srcd))
log_printf(logfile, "found %d file(s) to process.\\n", count my pp)
-- process target files
if (count my pp) > 0 then
if in_place then
-- archive the source directory first (zip file name = srcd_YYYY-MM-DDTHH.MM.SS.zip)
set dst to do shell script "src=" & srcd's quoted form & "
dst=\"${src}_$(date +'%FT%H.%M.%S').zip\"
ditto -ck --keepParent --sequesterRsrc \"$src\" \"$dst\"
echo \"$dst\""
log_printf(logfile, "archived source directory in %s\\n", dst)
repeat with p in my pp
set p to p's contents
set q to p & ext
log_printf(logfile, "converting %s => %s\\n", {p, q})
save_AW6WP_as_Pages4(p, q, {inherit_dates:inherit_dates})
--do shell script "rm -f " & p's quoted form -- delete the source file [1]
end repeat
else
set slen to count srcd
repeat with p in my pp
set p to p's contents
set q to dstd & (p's text (slen + 1) thru -1) & ext
log_printf(logfile, "converting %s => %s\\n", {p, q})
save_AW6WP_as_Pages4(p, q, {inherit_dates:inherit_dates})
end repeat
end if
end if
-- completion notice
log_printf(logfile, "process completed for total of %d file(s).\\n", count my pp)
tell me
activate
display dialog "Done " & (count my pp) & " file(s)." giving up after 5 with icon note
end tell
(*
[1] NOT recommended because conversion is not necessarily complete.
*)
end script
tell o to run
end _main
on scan_AW_files(d)
(*
string d : POSIX path of source directory where to start scanning
return list : list of POSIX paths of found files
* query condition is (file name extension = "cwk") OR (file creator type = "BOBO")
*)
script o
property pp : {}
property qq : {}
property rr : {}
tell application "System Events"
tell disk item d
set pp to every folder's POSIX path
repeat with p in my pp
set qq to my scan_AW_files(p's contents)
repeat with q in my qq
set end of my rr to q's contents
end repeat
end repeat
--set qq to every file's POSIX path whose name extension = "cwk" or (creator type = "BOBO" and file type = "CWWP")
set qq to every file's POSIX path whose name extension = "cwk" or creator type = "BOBO"
repeat with q in my qq
set end of my rr to q's contents
end repeat
end tell
end tell
return my rr's contents
end script
tell o to run
end scan_AW_files
on is_AW_v6_WP(f)
(*
string f : POSIX path of source file
return boolean : true if f is AW version 6 WP file, false otherwise
*)
(*
byte[1] = version
byte[5..8] = BOBO
byte[279] (when byte[1] = 0x06) =
0 => draw
1 => word processing
2 => spreadsheet
3 => database
4 => paint
5 => presentation
* byte index is 1-based
*)
set f to f as POSIX file
(* -- 10.5 or later only
return (read f from 5 for 4)'s id = {66, 79, 66, 79} and ¬
(read f for 1)'s id = 6 and ¬
(read f from 279 for 1)'s id = 1
*)
considering case
return (read f from 5 for 4) = "BOBO" and ¬
(ASCII number (read f for 1)) = 6 and ¬
(ASCII number (read f from 279 for 1)) = 1
end considering
end is_AW_v6_WP
on save_AW6WP_as_Pages4(src, dst, {inherit_dates:inherit_dates})
(*
string src : POSIX path of source file
string dst : POSIX path of destination file
boolean inherit_dates: true for dst to inherit creation and modification dates of src, false otherwise.
* src may equate to dst, in which case src is overwritten
* intermeditate directories in dst will be created as needed if not present
*)
-- get source and destination file
set srcf to src as POSIX file
set dstf to dst as POSIX file
-- open srca in Pages v4 and save it in dstf
tell application "Pages"
--activate
set k to count documents
open srcf
repeat until (count documents) > k
delay 0.2
end repeat
tell document 1
close saving in dstf
end tell
end tell
-- wait for dst to come into existence
wait_file_exist(dst)
-- set label of destination file to label of source file -- [1]
repeat 3 times -- max retry
try -- [2]
tell application "Finder"
set lbl to item (srcf as alias)'s label index
tell item (dstf as alias)
update
set its label index to lbl
end tell
end tell
exit repeat -- exit if no error
delay 0.5 -- wait some time before retry
end try
end repeat
-- inherit creation and modication dates from src to tmp
if inherit_dates then inherit_file_dates(src, dst)
(*
[1] This is required because Pages does not preserve the original label when saving file.
[2] Finder is such an unreliable beast that it may fail even after the file indeed has come into existence.
*)
end save_AW6WP_as_Pages4
on wait_file_exist(f)
(*
string f : POSIX path of file
* wait until f comes into existence
*)
do shell script "f=" & f's quoted form & "
until [[ -e \"$f\" ]]; do sleep 0.3; done"
end wait_file_exist
on _match(pat, aa)
(*
handler pat : handler to test elements in aa
list aa : source list
return list : list of every element a of list aa whose pat(a) = true
*)
script o
property |::xx| : aa's contents
property |::yy| : {}
property |::f| : pat
repeat with x in my |::xx|
set x to x's contents
if my |::f|(x) then set end of my |::yy| to x
end repeat
return my |::yy|'s contents
end script
tell o to run
end _match
on log_printf(f, fmt, lst)
(*
string f : POSIX path of log file
string fmt : printf format string
list lst : list of values (if lst is one item list {x}, lst may be x)
* %-26s time-stamp in format %F %T%z is added to the beginning of each entry
*)
local args
set args to "'%-26s'" & fmt's quoted form & " \"$(date +'%F %T%z')\" "
repeat with a in {} & lst
set args to args & (a as string)'s quoted form & space
end repeat
do shell script "printf " & args & " >> " & f's quoted form
end log_printf
on inherit_file_dates(src, dst)
(*
string src : POSIX path of source file
string dst : POSIX path of destination file
* If creation date of src is older than or equal to that of dst,
this will set both creation date and modification date of dst to those of src.
* If creation date of src is newer than that of dst,
this will set only modification date of dst to that of src.
*)
do shell script "src=" & src's quoted form & "; dst=" & dst's quoted form & "
ct=$(stat -f '%SB' -t '%Y%m%d%H%M.%S' \"$src\")
mt=$(stat -f '%Sm' -t '%Y%m%d%H%M.%S' \"$src\")
touch -t $ct \"$dst\" # set creation date (<= current creation date)
touch -mt $mt \"$dst\" # set modification date (>= creation date)"
end inherit_file_dates
-----------------------------------------------
--save_AW6_SS_as_Numbers2.applescript
(*
Convert AppleWorks v6 SS (spreadsheet) files to iWork'09's Numbers v2 files
v0.2
v0.2 -
changed the logging scheme from post-conversion log:
converted x => y
to pre-conversion log:
converting x => y
so that it can help to identify the last file which had caused the applicaton / script to crash.
v0.1 -
initial version
* Tested with Numbers 2.0.5 under OS X 10.6.8.
* Specify property in_place in script as necessary.
* Specify property inherit_dates in script as necessary.
* A log file with time-stamp in name is created on desktop.
* Original file name is always preserved except that name extension ".numbers" is added
Note that original name extension (.cwk) is NOT removed and thus, e.g., abc.cwk will be converted to abc.cwk.numbers
This is to avoid possible data loss in case there're file names, e.g., abc and abc.cwk in a source directory.
*)
_main()
on _main()
script o
property in_place : true
(*
true : convert files in place, i.e. converted files are saved in original directories,
where original source directory is archived in zip file with time-stamp in name in advance;
false : converted files are saved under destination directory which is specified in a dialogue in script
*)
property inherit_dates : true
(*
true : destination file inherits creation date and modification date of source file
false : destination file has creation date and modification date at the time of conversion
*)
property ext : ".numbers"
property logfile : (path to desktop)'s POSIX path & (do shell script "date +'save_AW6_SS_as_Numbers4.log_%Y%m%dT%H%M%S.txt'")
property pp : {}
-- accept source directory (and destination direcotry if in_place is false)
set srcd to (choose folder with prompt "Choose source directory where source files reside.")'s POSIX path
if srcd ends with "/" and (count srcd) > 1 then set srcd to srcd's text 1 thru -2
if not in_place then
repeat
set dstd to (choose folder with prompt "Choose destination directory where to save converted files.")'s POSIX path
if dstd ends with "/" and (count dstd) > 1 then set dstd to dstd's text 1 thru -2
if srcd ≠ dstd then exit repeat
display dialog "Source and destination directories cannot be the same!" with icon stop
end repeat
end if
log_printf(logfile, "conversion started in operation mode: in_place = %s, inherit_dates = %s.\\n", {in_place, inherit_dates})
-- retrieve target files
log_printf(logfile, "scanning files under %s\\n", srcd)
set pp to _match(my is_AW_v6_SS, scan_AW_files(srcd))
log_printf(logfile, "found %d file(s) to process.\\n", count my pp)
-- process target files
if (count my pp) > 0 then
if in_place then
-- archive the source directory first (zip file name = srcd_YYYY-MM-DDTHH.MM.SS.zip)
set dst to do shell script "src=" & srcd's quoted form & "
dst=\"${src}_$(date +'%FT%H.%M.%S').zip\"
ditto -ck --keepParent --sequesterRsrc \"$src\" \"$dst\"
echo \"$dst\""
log_printf(logfile, "archived source directory in %s\\n", dst)
repeat with p in my pp
set p to p's contents
set q to p & ext
log_printf(logfile, "converting %s => %s\\n", {p, q})
save_AW6SS_as_Numbers2(p, q, {inherit_dates:inherit_dates})
--do shell script "rm -f " & p's quoted form -- delete the source file [1]
end repeat
else
set slen to count srcd
repeat with p in my pp
set p to p's contents
set q to dstd & (p's text (slen + 1) thru -1) & ext
log_printf(logfile, "converting %s => %s\\n", {p, q})
save_AW6SS_as_Numbers2(p, q, {inherit_dates:inherit_dates})
end repeat
end if
end if
-- completion notice
log_printf(logfile, "process completed for total of %d file(s).\\n", count my pp)
tell me
activate
display dialog "Done " & (count my pp) & " file(s)." giving up after 5 with icon note
end tell
(*
[1] NOT recommended because conversion is not necessarily complete.
*)
end script
tell o to run
end _main
on scan_AW_files(d)
(*
string d : POSIX path of source directory where to start scanning
return list : list of POSIX paths of found files
* query condition is (file name extension = "cwk") OR (file creator type = "BOBO")
*)
script o
property pp : {}
property qq : {}
property rr : {}
tell application "System Events"
tell disk item d
set pp to every folder's POSIX path
repeat with p in my pp
set qq to my scan_AW_files(p's contents)
repeat with q in my qq
set end of my rr to q's contents
end repeat
end repeat
--set qq to every file's POSIX path whose name extension = "cwk" or (creator type = "BOBO" and file type = "CWSS")
set qq to every file's POSIX path whose name extension = "cwk" or creator type = "BOBO"
repeat with q in my qq
set end of my rr to q's contents
end repeat
end tell
end tell
return my rr's contents
end script
tell o to run
end scan_AW_files
on is_AW_v6_SS(f)
(*
string f : POSIX path of source file
return boolean : true if f is AW version 6 WP file, false otherwise
*)
(*
byte[1] = version
byte[5..8] = BOBO
byte[279] (when byte[1] = 0x06) =
0 => draw
1 => word processing
2 => spreadsheet
3 => database
4 => paint
5 => presentation
* byte index is 1-based
*)
set f to f as POSIX file
(* -- 10.5 or later only
return (read f from 5 for 4)'s id = {66, 79, 66, 79} and ¬
(read f for 1)'s id = 6 and ¬
(read f from 279 for 1)'s id = 1
*)
considering case
return (read f from 5 for 4) = "BOBO" and ¬
(ASCII number (read f for 1)) = 6 and ¬
(ASCII number (read f from 279 for 1)) = 2
end considering
end is_AW_v6_SS
on save_AW6SS_as_Numbers2(src, dst, {inherit_dates:inherit_dates})
(*
string src : POSIX path of source file
string dst : POSIX path of destination file
boolean inherit_dates: true for dst to inherit creation and modification dates of src, false otherwise.
* src may equate to dst, in which case src is overwritten
* intermeditate directories in dst will be created as needed if not present
*)
-- get source and destination file
set srcf to src as POSIX file
set dstf to dst as POSIX file
-- open srca in Pages v4 and save it in dstf
tell application "Numbers"
--activate
set k to count documents
open srcf
repeat until (count documents) > k
delay 0.2
end repeat
tell document 1
close saving in dstf
end tell
end tell
-- wait for dst to come into existence
wait_file_exist(dst)
-- set label of destination file to label of source file -- [1]
repeat 3 times -- max retry
try -- [2]
tell application "Finder"
set lbl to item (srcf as alias)'s label index
tell item (dstf as alias)
update
set its label index to lbl
end tell
end tell
exit repeat -- exit if no error
delay 0.5 -- wait some time before retry
end try
end repeat
-- inherit creation and modication dates from src to tmp
if inherit_dates then inherit_file_dates(src, dst)
(*
[1] This is required because Pages does not preserve the original label when saving file.
[2] Finder is such an unreliable beast that it may fail even after the file indeed has come into existence.
*)
end save_AW6SS_as_Numbers2
on wait_file_exist(f)
(*
string f : POSIX path of file
* wait until f comes into existence
*)
do shell script "f=" & f's quoted form & "
until [[ -e \"$f\" ]]; do sleep 0.3; done"
end wait_file_exist
on _match(pat, aa)
(*
handler pat : handler to test elements in aa
list aa : source list
return list : list of every element a of list aa whose pat(a) = true
*)
script o
property |::xx| : aa's contents
property |::yy| : {}
property |::f| : pat
repeat with x in my |::xx|
set x to x's contents
if my |::f|(x) then set end of my |::yy| to x
end repeat
return my |::yy|'s contents
end script
tell o to run
end _match
on log_printf(f, fmt, lst)
(*
string f : POSIX path of log file
string fmt : printf format string
list lst : list of values (if lst is one item list {x}, lst may be x)
* %-26s time-stamp in format %F %T%z is added to the beginning of each entry
*)
local args
set args to "'%-26s'" & fmt's quoted form & " \"$(date +'%F %T%z')\" "
repeat with a in {} & lst
set args to args & (a as string)'s quoted form & space
end repeat
do shell script "printf " & args & " >> " & f's quoted form
end log_printf
on inherit_file_dates(src, dst)
(*
string src : POSIX path of source file
string dst : POSIX path of destination file
* If creation date of src is older than or equal to that of dst,
this will set both creation date and modification date of dst to those of src.
* If creation date of src is newer than that of dst,
this will set only modification date of dst to that of src.
*)
do shell script "src=" & src's quoted form & "; dst=" & dst's quoted form & "
ct=$(stat -f '%SB' -t '%Y%m%d%H%M.%S' \"$src\")
mt=$(stat -f '%Sm' -t '%Y%m%d%H%M.%S' \"$src\")
touch -t $ct \"$dst\" # set creation date (<= current creation date)
touch -mt $mt \"$dst\" # set modification date (>= creation date)"
end inherit_file_dates
-----------------------------------------------
--save_AW_as_v6.applescript
(*
Convert files of AppleWorks / ClarisWorks prior to version 6 to AppleWorks 6 files.
v0.3
v0.3 -
changed the logging scheme from post-conversion log:
converted x => y
to pre-conversion log:
converting x => y
so that it can help to identify the last file which had caused the applicaton / script to crash.
v0.2 -
added optional function to inherit creation and modification dates of source files
v0.1 -
initial version
* Tested with AppleWorks 6.2.4 under OS X 10.6.8.
* AppleWorks 6.2.9 may fail to open old ClarisWorks files, in which case use older version such as AppleWorks 6.2.4.
* Specify property in_place in script as necessary.
* Specify property inherit_dates in script as necessary.
* A log file with time-stamp in name is created on desktop.
* Original file name is always preserved.
Name extension ".cwk" is not added in destination file if it is missing in source file.
This is to avoid possible data loss in case there're file names, e.g., abc and abc.cwk in a source directory.
*)
_main()
on _main()
script o
property in_place : true
(*
true : convert files in place, i.e. files are overwritten,
where original source directory is archived in zip file with time-stamp in name in advance;
false : converted files are saved under destination directory which is specified in a dialogue in script
*)
property inherit_dates : true
(*
true : destination file inherits creation date and modification date of source file
false : destination file has creation date and modification date at the time of conversion
*)
property logfile : (path to desktop)'s POSIX path & (do shell script "date +'save_AW_as_v6.log_%Y%m%dT%H%M%S.txt'")
property pp : {}
-- accept source directory (and destination direcotry if in_place is false)
set srcd to (choose folder with prompt "Choose source directory where source files reside.")'s POSIX path
if srcd ends with "/" and (count srcd) > 1 then set srcd to srcd's text 1 thru -2
if not in_place then
repeat
set dstd to (choose folder with prompt "Choose destination directory where to save converted files.")'s POSIX path
if dstd ends with "/" and (count dstd) > 1 then set dstd to dstd's text 1 thru -2
if srcd ≠ dstd then exit repeat
display dialog "Source and destination directories cannot be the same!" with icon stop
end repeat
end if
log_printf(logfile, "conversion started in operation mode: in_place = %s, inherit_dates = %s.\\n", {in_place, inherit_dates})
-- retrieve target files
log_printf(logfile, "scanning files under %s\\n", srcd)
set pp to _match(my is_AW_but_v6, scan_AW_files(srcd))
log_printf(logfile, "found %d file(s) to process.\\n", count my pp)
-- process target files
if (count my pp) > 0 then
if in_place then
-- archive the source directory first (zip file name = srcd_YYYY-MM-DDTHH.MM.SS.zip)
set dst to do shell script "src=" & srcd's quoted form & "
dst=\"${src}_$(date +'%FT%H.%M.%S').zip\"
ditto -ck --keepParent --sequesterRsrc \"$src\" \"$dst\"
echo \"$dst\""
log_printf(logfile, "archived source directory in %s\\n", dst)
repeat with p in my pp
set p to p's contents
log_printf(logfile, "converting %s\\n", p)
save_AW_as_v6(p, p, {inherit_dates:inherit_dates})
end repeat
else
set slen to count srcd
repeat with p in my pp
set p to p's contents
set q to dstd & (p's text (slen + 1) thru -1)
log_printf(logfile, "converting %s => %s\\n", {p, q})
save_AW_as_v6(p, q, {inherit_dates:inherit_dates})
end repeat
end if
end if
-- completion notice
log_printf(logfile, "process completed for total of %d file(s).\\n", count my pp)
tell me
activate
display dialog "Done " & (count my pp) & " file(s)." giving up after 5 with icon note
end tell
end script
tell o to run
end _main
on scan_AW_files(d)
(*
string d : POSIX path of source directory where to start scanning
return list : list of POSIX paths of found files
* query condition is (file name extension = "cwk") OR (file creator type = "BOBO")
*)
script o
property pp : {}
property qq : {}
property rr : {}
tell application "System Events"
tell disk item d
set pp to every folder's POSIX path
repeat with p in my pp
set qq to my scan_AW_files(p's contents)
repeat with q in my qq
set end of my rr to q's contents
end repeat
end repeat
set qq to every file's POSIX path whose name extension = "cwk" or creator type = "BOBO"
repeat with q in my qq
set end of my rr to q's contents
end repeat
end tell
end tell
return my rr's contents
end script
tell o to run
end scan_AW_files
on is_AW_but_v6(f)
(*
string f : POSIX path of source file
return boolean : true if f is AW/CW file of version prior to 6, false otherwise
* matching codition is (byte 1 < 0x06) AND (bytes 5..8 = 'BOBO')
*)
set f to f as POSIX file --as alias
--return (read f from 5 for 4)'s id = {66, 79, 66, 79} and (read f for 1)'s id < 6 -- for 10.5 or later only
considering case
return (read f from 5 for 4) = "BOBO" and (ASCII number (read f for 1)) < 6
end considering
end is_AW_but_v6
on save_AW_as_v6(src, dst, {inherit_dates:inherit_dates})
(*
string src : POSIX path of source file, typically file of AW5 or CW4 etc
string dst : POSIX path of destination file
boolean inherit_dates: true for dst to inherit creation and modification dates of src, false otherwise.
* src may equate to dst, in which case src is overwritten
* intermeditate directories in dst will be created as needed if not present
*)
-- get source alias
set srca to src as POSIX file as alias
-- create temp file
set tmp to do shell script "mktemp /tmp/save_AW_as_v6.XXXXXXXX"
set tmpa to tmp as POSIX file as alias
-- convert to AW6 and save in temp file
tell application "AppleWorks 6"
--activate
set k to count documents
open srca
repeat until (count documents) > k
delay 0.2
end repeat
tell document 1
close saving in tmpa
end tell
end tell
-- wait for the temp file is closed
wait_file_close(tmp)
-- set label of temp file to label of source file -- [1]
repeat 3 times -- max retry
try -- [2]
tell application "Finder"
set lbl to item srca's label index
tell item tmpa
update
set its label index to lbl
end tell
end tell
exit repeat -- exit if no error
delay 0.5 -- wait some time before retry
end try
end repeat
-- inherit creation and modication dates from src to tmp
if inherit_dates then inherit_file_dates(src, tmp)
-- move temp file to destination file (destination directory tree is created as necessary)
do shell script "tmp=" & tmp's quoted form & "; dst=" & dst's quoted form & "
d=${dst%/*}; [[ -d \"$d\" ]] || mkdir -p \"$d\"
mv -f \"$tmp\" \"$dst\""
(*
[1] This is required because AW6 does not preserve the original label when saving file.
[2] Finder is such an unreliable beast that it may fail even after the file is indeed closed.
*)
end save_AW_as_v6
on wait_file_close(f)
(*
string f : POSIX path of file
* wait until f is no longer opened by AppleWorks
*)
do shell script "f=" & f's quoted form & "
while [[ $(lsof -Fc \"$f\") =~ 'AppleWorks' ]]; do sleep 0.3; done"
end wait_file_close
on _match(pat, aa)
(*
handler pat : handler to test elements in aa
list aa : source list
return list : list of every element a of list aa whose pat(a) = true
*)
script o
property |::xx| : aa's contents
property |::yy| : {}
property |::f| : pat
repeat with x in my |::xx|
set x to x's contents
if my |::f|(x) then set end of my |::yy| to x
end repeat
return my |::yy|'s contents
end script
tell o to run
end _match
on log_printf(f, fmt, lst)
(*
string f : POSIX path of log file
string fmt : printf format string
list lst : list of values (if lst is one item list {x}, lst may be x)
* %-26s time-stamp in format %F %T%z is added to the beginning of each entry
*)
local args
set args to "'%-26s'" & fmt's quoted form & " \"$(date +'%F %T%z')\" "
repeat with a in {} & lst
set args to args & (a as string)'s quoted form & space
end repeat
do shell script "printf " & args & " >> " & f's quoted form
end log_printf
on inherit_file_dates(src, dst)
(*
string src : POSIX path of source file
string dst : POSIX path of destination file
* If creation date of src is older than or equal to that of dst,
this will set both creation date and modification date of dst to those of src.
* If creation date of src is newer than that of dst,
this will set only modification date of dst to that of src.
*)
do shell script "src=" & src's quoted form & "; dst=" & dst's quoted form & "
ct=$(stat -f '%SB' -t '%Y%m%d%H%M.%S' \"$src\")
mt=$(stat -f '%Sm' -t '%Y%m%d%H%M.%S' \"$src\")
touch -t $ct \"$dst\" # set creation date (<= current creation date)
touch -mt $mt \"$dst\" # set modification date (>= creation date)"
end inherit_file_dates