How to run a script

Hope someone can answer this ASAP, I'm on deadline.

I have to convert .cwk files to Appleworks6, and found some great scripts by Hiroto in the AW forum.

However, i dont know how to use them!

copy and paste into script editor? Terminal? And then what? Compile? Run? ????

the confusing thing for me is none of them have the word "tell" in them, which all my research has informed me is how an applescript script reads...

Just (* , etc.

Here is the first part of one:




  1. -- save_AW_as_v6.applescript
  2. (*
  3. Convert files of AppleWorks / ClarisWorks prior to version 6 to AppleWorks 6 files.
  4. v0.1
  5. * Tested with AppleWorks 6.2.4 under OS X 10.6.8.
  6. * AppleWorks 6.2.9 may fail to open old ClarisWorks files, in which case use older version such as AppleWorks 6.2.4.
  7. * Specify property in_place in script, which defines the operation mode, as necessary.
  8. * A log file with time-stamp in name is created on desktop.
  9. * Original file name is always preserved.
  10. Name extension ".cwk" is not added in destination file if it is missing in source file.
  11. This is to avoid possible data loss in case there're file names, e.g., abc and abc.cwk in a source directory.
  12. *)
  13. _main()
  14. on _main()
  15. script o
  16. property in_place : false
  17. (*
  18. true : convert files in place, i.e. files are overwritten,
  19. where original source directory is archived in zip file with time-stamp in name in advance;
  20. false : converted files are saved in destination directory which is specified in a dialogue in script
  21. *)

Posted on Feb 17, 2015 11:44 AM

Reply
14 replies

Feb 17, 2015 1:49 PM in response to missknowitallsoshe thinks...

Did that and keep getting errors. here's the latest. I copied this from a web page into text edit. Is there something I need to do to convert it before bringing it into Script Editor?

This is getting tiresome...

It wants me to get rid of spaces and then throws me something like this ( I don't know what an "end of line" would look like/be...

User uploaded file

Feb 17, 2015 2:27 PM in response to missknowitallsoshe thinks...

Caught by the editor timeout here.


The correct syntax is:


_main()


on _main()

script o

property in_place : true

other script contents

end script

end _main


I see you are using the v0.1 edition of Hiroto's script. There is an updated v0.2 here.


After the script, there is a statement, tell o to run, which runs the script contents. The _main() acts just like a function call and runs the script via the tell statement.

Feb 17, 2015 2:28 PM in response to missknowitallsoshe thinks...

Thanks, for your time with this, Niel. I really don't want to have to convert them all by hand, so if I can get these to work, it would be beyond wonderful. I'm 2 days past when i told the client I'd have them ready...

Here are all 3 scripts. Cwk to AW6, AW6 to Numbers, AW6 to Pages...i imagine I'll have the same trouble with all of them.

Here's Hiroto's original posting:https://discussions.apple.com/thread/5455245?start=45&tstart=0



  1. --save_AW_as_v6.applescript
  2. (*
  3. Convert files of AppleWorks / ClarisWorks prior to version 6 to AppleWorks 6 files.
  4. v0.3
  5. v0.3 -
  6. changed the logging scheme from post-conversion log:
  7. converted x => y
  8. to pre-conversion log:
  9. converting x => y
  10. so that it can help to identify the last file which had caused the applicaton / script to crash.
  11. v0.2 -
  12. added optional function to inherit creation and modification dates of source files
  13. v0.1 -
  14. initial version
  15. * Tested with AppleWorks 6.2.4 under OS X 10.6.8.
  16. * AppleWorks 6.2.9 may fail to open old ClarisWorks files, in which case use older version such as AppleWorks 6.2.4.
  17. * Specify property in_place in script as necessary.
  18. * Specify property inherit_dates in script as necessary.
  19. * A log file with time-stamp in name is created on desktop.
  20. * Original file name is always preserved.
  21. Name extension ".cwk" is not added in destination file if it is missing in source file.
  22. This is to avoid possible data loss in case there're file names, e.g., abc and abc.cwk in a source directory.
  23. *)
  24. _main()
  25. on _main()
  26. script o
  27. property in_place : true
  28. (*
  29. true : convert files in place, i.e. files are overwritten,
  30. where original source directory is archived in zip file with time-stamp in name in advance;
  31. false : converted files are saved under destination directory which is specified in a dialogue in script
  32. *)
  33. property inherit_dates : true
  34. (*
  35. true : destination file inherits creation date and modification date of source file
  36. false : destination file has creation date and modification date at the time of conversion
  37. *)
  38. property logfile : (path to desktop)'s POSIX path & (do shell script "date +'save_AW_as_v6.log_%Y%m%dT%H%M%S.txt'")
  39. property pp : {}
  40. -- accept source directory (and destination direcotry if in_place is false)
  41. set srcd to (choose folder with prompt "Choose source directory where source files reside.")'s POSIX path
  42. if srcd ends with "/" and (count srcd) > 1 then set srcd to srcd's text 1 thru -2
  43. if not in_place then
  44. repeat
  45. set dstd to (choose folder with prompt "Choose destination directory where to save converted files.")'s POSIX path
  46. if dstd ends with "/" and (count dstd) > 1 then set dstd to dstd's text 1 thru -2
  47. if srcd ≠ dstd then exit repeat
  48. display dialog "Source and destination directories cannot be the same!" with icon stop
  49. end repeat
  50. end if
  51. log_printf(logfile, "conversion started in operation mode: in_place = %s, inherit_dates = %s.\\n", {in_place, inherit_dates})
  52. -- retrieve target files
  53. log_printf(logfile, "scanning files under %s\\n", srcd)
  54. set pp to _match(my is_AW_but_v6, scan_AW_files(srcd))
  55. log_printf(logfile, "found %d file(s) to process.\\n", count my pp)
  56. -- process target files
  57. if (count my pp) > 0 then
  58. if in_place then
  59. -- archive the source directory first (zip file name = srcd_YYYY-MM-DDTHH.MM.SS.zip)
  60. set dst to do shell script "src=" & srcd's quoted form & "
  61. dst=\"${src}_$(date +'%FT%H.%M.%S').zip\"
  62. ditto -ck --keepParent --sequesterRsrc \"$src\" \"$dst\"
  63. echo \"$dst\""
  64. log_printf(logfile, "archived source directory in %s\\n", dst)
  65. repeat with p in my pp
  66. set p to p's contents
  67. log_printf(logfile, "converting %s\\n", p)
  68. save_AW_as_v6(p, p, {inherit_dates:inherit_dates})
  69. end repeat
  70. else
  71. set slen to count srcd
  72. repeat with p in my pp
  73. set p to p's contents
  74. set q to dstd & (p's text (slen + 1) thru -1)
  75. log_printf(logfile, "converting %s => %s\\n", {p, q})
  76. save_AW_as_v6(p, q, {inherit_dates:inherit_dates})
  77. end repeat
  78. end if
  79. end if
  80. -- completion notice
  81. log_printf(logfile, "process completed for total of %d file(s).\\n", count my pp)
  82. tell me
  83. activate
  84. display dialog "Done " & (count my pp) & " file(s)." giving up after 5 with icon note
  85. end tell
  86. end script
  87. tell o to run
  88. end _main
  89. on scan_AW_files(d)
  90. (*
  91. string d : POSIX path of source directory where to start scanning
  92. return list : list of POSIX paths of found files
  93. * query condition is (file name extension = "cwk") OR (file creator type = "BOBO")
  94. *)
  95. script o
  96. property pp : {}
  97. property qq : {}
  98. property rr : {}
  99. tell application "System Events"
  100. tell disk item d
  101. set pp to every folder's POSIX path
  102. repeat with p in my pp
  103. set qq to my scan_AW_files(p's contents)
  104. repeat with q in my qq
  105. set end of my rr to q's contents
  106. end repeat
  107. end repeat
  108. set qq to every file's POSIX path whose name extension = "cwk" or creator type = "BOBO"
  109. repeat with q in my qq
  110. set end of my rr to q's contents
  111. end repeat
  112. end tell
  113. end tell
  114. return my rr's contents
  115. end script
  116. tell o to run
  117. end scan_AW_files
  118. on is_AW_but_v6(f)
  119. (*
  120. string f : POSIX path of source file
  121. return boolean : true if f is AW/CW file of version prior to 6, false otherwise
  122. * matching codition is (byte 1 < 0x06) AND (bytes 5..8 = 'BOBO')
  123. *)
  124. set f to f as POSIX file --as alias
  125. --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
  126. considering case
  127. return (read f from 5 for 4) = "BOBO" and (ASCII number (read f for 1)) < 6
  128. end considering
  129. end is_AW_but_v6
  130. on save_AW_as_v6(src, dst, {inherit_dates:inherit_dates})
  131. (*
  132. string src : POSIX path of source file, typically file of AW5 or CW4 etc
  133. string dst : POSIX path of destination file
  134. boolean inherit_dates: true for dst to inherit creation and modification dates of src, false otherwise.
  135. * src may equate to dst, in which case src is overwritten
  136. * intermeditate directories in dst will be created as needed if not present
  137. *)
  138. -- get source alias
  139. set srca to src as POSIX file as alias
  140. -- create temp file
  141. set tmp to do shell script "mktemp /tmp/save_AW_as_v6.XXXXXXXX"
  142. set tmpa to tmp as POSIX file as alias
  143. -- convert to AW6 and save in temp file
  144. tell application "AppleWorks 6"
  145. --activate
  146. set k to count documents
  147. open srca
  148. repeat until (count documents) > k
  149. delay 0.2
  150. end repeat
  151. tell document 1
  152. close saving in tmpa
  153. end tell
  154. end tell
  155. -- wait for the temp file is closed
  156. wait_file_close(tmp)
  157. -- set label of temp file to label of source file -- [1]
  158. repeat 3 times -- max retry
  159. try -- [2]
  160. tell application "Finder"
  161. set lbl to item srca's label index
  162. tell item tmpa
  163. update
  164. set its label index to lbl
  165. end tell
  166. end tell
  167. exit repeat -- exit if no error
  168. delay 0.5 -- wait some time before retry
  169. end try
  170. end repeat
  171. -- inherit creation and modication dates from src to tmp
  172. if inherit_dates then inherit_file_dates(src, tmp)
  173. -- move temp file to destination file (destination directory tree is created as necessary)
  174. do shell script "tmp=" & tmp's quoted form & "; dst=" & dst's quoted form & "
  175. d=${dst%/*}; [[ -d \"$d\" ]] || mkdir -p \"$d\"
  176. mv -f \"$tmp\" \"$dst\""
  177. (*
  178. [1] This is required because AW6 does not preserve the original label when saving file.
  179. [2] Finder is such an unreliable beast that it may fail even after the file is indeed closed.
  180. *)
  181. end save_AW_as_v6
  182. on wait_file_close(f)
  183. (*
  184. string f : POSIX path of file
  185. * wait until f is no longer opened by AppleWorks
  186. *)
  187. do shell script "f=" & f's quoted form & "
  188. while [[ $(lsof -Fc \"$f\") =~ 'AppleWorks' ]]; do sleep 0.3; done"
  189. end wait_file_close
  190. on _match(pat, aa)
  191. (*
  192. handler pat : handler to test elements in aa
  193. list aa : source list
  194. return list : list of every element a of list aa whose pat(a) = true
  195. *)
  196. script o
  197. property |::xx| : aa's contents
  198. property |::yy| : {}
  199. property |::f| : pat
  200. repeat with x in my |::xx|
  201. set x to x's contents
  202. if my |::f|(x) then set end of my |::yy| to x
  203. end repeat
  204. return my |::yy|'s contents
  205. end script
  206. tell o to run
  207. end _match
  208. on log_printf(f, fmt, lst)
  209. (*
  210. string f : POSIX path of log file
  211. string fmt : printf format string
  212. list lst : list of values (if lst is one item list {x}, lst may be x)
  213. * %-26s time-stamp in format %F %T%z is added to the beginning of each entry
  214. *)
  215. local args
  216. set args to "'%-26s'" & fmt's quoted form & " \"$(date +'%F %T%z')\" "
  217. repeat with a in {} & lst
  218. set args to args & (a as string)'s quoted form & space
  219. end repeat
  220. do shell script "printf " & args & " >> " & f's quoted form
  221. end log_printf
  222. on inherit_file_dates(src, dst)
  223. (*
  224. string src : POSIX path of source file
  225. string dst : POSIX path of destination file
  226. * If creation date of src is older than or equal to that of dst,
  227. this will set both creation date and modification date of dst to those of src.
  228. * If creation date of src is newer than that of dst,
  229. this will set only modification date of dst to that of src.
  230. *)
  231. do shell script "src=" & src's quoted form & "; dst=" & dst's quoted form & "
  232. ct=$(stat -f '%SB' -t '%Y%m%d%H%M.%S' \"$src\")
  233. mt=$(stat -f '%Sm' -t '%Y%m%d%H%M.%S' \"$src\")
  234. touch -t $ct \"$dst\" # set creation date (<= current creation date)
  235. touch -mt $mt \"$dst\" # set modification date (>= creation date)"
  236. end inherit_file_dates





[CodeBlockStart:17c5a62c-54d3-4061-ac41-37b7e4ed1ce1][excluded]


  1. --save_AW6_SS_as_Numbers2.applescript
  2. (*
  3. Convert AppleWorks v6 SS (spreadsheet) files to iWork'09's Numbers v2 files
  4. v0.2
  5. v0.2 -
  6. changed the logging scheme from post-conversion log:
  7. converted x => y
  8. to pre-conversion log:
  9. converting x => y
  10. so that it can help to identify the last file which had caused the applicaton / script to crash.

  11. v0.1 -
  12. initial version
  13. * Tested with Numbers 2.0.5 under OS X 10.6.8.
  14. * Specify property in_place in script as necessary.
  15. * Specify property inherit_dates in script as necessary.
  16. * A log file with time-stamp in name is created on desktop.
  17. * Original file name is always preserved except that name extension ".numbers" is added
  18. Note that original name extension (.cwk) is NOT removed and thus, e.g., abc.cwk will be converted to abc.cwk.numbers
  19. This is to avoid possible data loss in case there're file names, e.g., abc and abc.cwk in a source directory.
  20. *)
  21. _main()
  22. on _main()
  23. script o
  24. property in_place : true
  25. (*
  26. true : convert files in place, i.e. converted files are saved in original directories,
  27. where original source directory is archived in zip file with time-stamp in name in advance;
  28. false : converted files are saved under destination directory which is specified in a dialogue in script
  29. *)
  30. property inherit_dates : true
  31. (*
  32. true : destination file inherits creation date and modification date of source file
  33. false : destination file has creation date and modification date at the time of conversion
  34. *)
  35. property ext : ".numbers"
  36. 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'")
  37. property pp : {}
  38. -- accept source directory (and destination direcotry if in_place is false)
  39. set srcd to (choose folder with prompt "Choose source directory where source files reside.")'s POSIX path
  40. if srcd ends with "/" and (count srcd) > 1 then set srcd to srcd's text 1 thru -2
  41. if not in_place then
  42. repeat
  43. set dstd to (choose folder with prompt "Choose destination directory where to save converted files.")'s POSIX path
  44. if dstd ends with "/" and (count dstd) > 1 then set dstd to dstd's text 1 thru -2
  45. if srcd ≠ dstd then exit repeat
  46. display dialog "Source and destination directories cannot be the same!" with icon stop
  47. end repeat
  48. end if
  49. log_printf(logfile, "conversion started in operation mode: in_place = %s, inherit_dates = %s.\\n", {in_place, inherit_dates})
  50. -- retrieve target files
  51. log_printf(logfile, "scanning files under %s\\n", srcd)
  52. set pp to _match(my is_AW_v6_SS, scan_AW_files(srcd))
  53. log_printf(logfile, "found %d file(s) to process.\\n", count my pp)
  54. -- process target files
  55. if (count my pp) > 0 then
  56. if in_place then
  57. -- archive the source directory first (zip file name = srcd_YYYY-MM-DDTHH.MM.SS.zip)
  58. set dst to do shell script "src=" & srcd's quoted form & "
  59. dst=\"${src}_$(date +'%FT%H.%M.%S').zip\"
  60. ditto -ck --keepParent --sequesterRsrc \"$src\" \"$dst\"
  61. echo \"$dst\""
  62. log_printf(logfile, "archived source directory in %s\\n", dst)
  63. repeat with p in my pp
  64. set p to p's contents
  65. set q to p & ext
  66. log_printf(logfile, "converting %s => %s\\n", {p, q})
  67. save_AW6SS_as_Numbers2(p, q, {inherit_dates:inherit_dates})
  68. --do shell script "rm -f " & p's quoted form -- delete the source file [1]
  69. end repeat
  70. else
  71. set slen to count srcd
  72. repeat with p in my pp
  73. set p to p's contents
  74. set q to dstd & (p's text (slen + 1) thru -1) & ext
  75. log_printf(logfile, "converting %s => %s\\n", {p, q})
  76. save_AW6SS_as_Numbers2(p, q, {inherit_dates:inherit_dates})
  77. end repeat
  78. end if
  79. end if
  80. -- completion notice
  81. log_printf(logfile, "process completed for total of %d file(s).\\n", count my pp)
  82. tell me
  83. activate
  84. display dialog "Done " & (count my pp) & " file(s)." giving up after 5 with icon note
  85. end tell
  86. (*
  87. [1] NOT recommended because conversion is not necessarily complete.
  88. *)
  89. end script
  90. tell o to run
  91. end _main

  92. on scan_AW_files(d)
  93. (*
  94. string d : POSIX path of source directory where to start scanning
  95. return list : list of POSIX paths of found files
  96. * query condition is (file name extension = "cwk") OR (file creator type = "BOBO")
  97. *)
  98. script o
  99. property pp : {}
  100. property qq : {}
  101. property rr : {}
  102. tell application "System Events"
  103. tell disk item d
  104. set pp to every folder's POSIX path
  105. repeat with p in my pp
  106. set qq to my scan_AW_files(p's contents)
  107. repeat with q in my qq
  108. set end of my rr to q's contents
  109. end repeat
  110. end repeat
  111. --set qq to every file's POSIX path whose name extension = "cwk" or (creator type = "BOBO" and file type = "CWSS")
  112. set qq to every file's POSIX path whose name extension = "cwk" or creator type = "BOBO"
  113. repeat with q in my qq
  114. set end of my rr to q's contents
  115. end repeat
  116. end tell
  117. end tell
  118. return my rr's contents
  119. end script
  120. tell o to run
  121. end scan_AW_files

  122. on is_AW_v6_SS(f)
  123. (*
  124. string f : POSIX path of source file
  125. return boolean : true if f is AW version 6 WP file, false otherwise
  126. *)
  127. (*
  128. byte[1] = version
  129. byte[5..8] = BOBO
  130. byte[279] (when byte[1] = 0x06) =
  131. 0 => draw
  132. 1 => word processing
  133. 2 => spreadsheet
  134. 3 => database
  135. 4 => paint
  136. 5 => presentation
  137. * byte index is 1-based
  138. *)
  139. set f to f as POSIX file
  140. (* -- 10.5 or later only
  141. return (read f from 5 for 4)'s id = {66, 79, 66, 79} and ¬
  142. (read f for 1)'s id = 6 and ¬
  143. (read f from 279 for 1)'s id = 1
  144. *)
  145. considering case
  146. return (read f from 5 for 4) = "BOBO" and ¬
  147. (ASCII number (read f for 1)) = 6 and ¬
  148. (ASCII number (read f from 279 for 1)) = 2
  149. end considering
  150. end is_AW_v6_SS

  151. on save_AW6SS_as_Numbers2(src, dst, {inherit_dates:inherit_dates})
  152. (*
  153. string src : POSIX path of source file
  154. string dst : POSIX path of destination file
  155. boolean inherit_dates: true for dst to inherit creation and modification dates of src, false otherwise.
  156. * src may equate to dst, in which case src is overwritten
  157. * intermeditate directories in dst will be created as needed if not present
  158. *)
  159. -- get source and destination file
  160. set srcf to src as POSIX file
  161. set dstf to dst as POSIX file
  162. -- open srca in Pages v4 and save it in dstf
  163. tell application "Numbers"
  164. --activate
  165. set k to count documents
  166. open srcf
  167. repeat until (count documents) > k
  168. delay 0.2
  169. end repeat
  170. tell document 1
  171. close saving in dstf
  172. end tell
  173. end tell
  174. -- wait for dst to come into existence
  175. wait_file_exist(dst)
  176. -- set label of destination file to label of source file -- [1]
  177. repeat 3 times -- max retry
  178. try -- [2]
  179. tell application "Finder"
  180. set lbl to item (srcf as alias)'s label index
  181. tell item (dstf as alias)
  182. update
  183. set its label index to lbl
  184. end tell
  185. end tell
  186. exit repeat -- exit if no error
  187. delay 0.5 -- wait some time before retry
  188. end try
  189. end repeat
  190. -- inherit creation and modication dates from src to tmp
  191. if inherit_dates then inherit_file_dates(src, dst)
  192. (*
  193. [1] This is required because Pages does not preserve the original label when saving file.
  194. [2] Finder is such an unreliable beast that it may fail even after the file indeed has come into existence.
  195. *)
  196. end save_AW6SS_as_Numbers2

  197. on wait_file_exist(f)
  198. (*
  199. string f : POSIX path of file
  200. * wait until f comes into existence
  201. *)
  202. do shell script "f=" & f's quoted form & "
  203. until [[ -e \"$f\" ]]; do sleep 0.3; done"
  204. end wait_file_exist

  205. on _match(pat, aa)
  206. (*
  207. handler pat : handler to test elements in aa
  208. list aa : source list
  209. return list : list of every element a of list aa whose pat(a) = true
  210. *)
  211. script o
  212. property |::xx| : aa's contents
  213. property |::yy| : {}
  214. property |::f| : pat
  215. repeat with x in my |::xx|
  216. set x to x's contents
  217. if my |::f|(x) then set end of my |::yy| to x
  218. end repeat
  219. return my |::yy|'s contents
  220. end script
  221. tell o to run
  222. end _match

  223. on log_printf(f, fmt, lst)
  224. (*
  225. string f : POSIX path of log file
  226. string fmt : printf format string
  227. list lst : list of values (if lst is one item list {x}, lst may be x)
  228. * %-26s time-stamp in format %F %T%z is added to the beginning of each entry
  229. *)
  230. local args
  231. set args to "'%-26s'" & fmt's quoted form & " \"$(date +'%F %T%z')\" "
  232. repeat with a in {} & lst
  233. set args to args & (a as string)'s quoted form & space
  234. end repeat
  235. do shell script "printf " & args & " >> " & f's quoted form
  236. end log_printf

  237. on inherit_file_dates(src, dst)
  238. (*
  239. string src : POSIX path of source file
  240. string dst : POSIX path of destination file
  241. * If creation date of src is older than or equal to that of dst,
  242. this will set both creation date and modification date of dst to those of src.
  243. * If creation date of src is newer than that of dst,
  244. this will set only modification date of dst to that of src.
  245. *)
  246. do shell script "src=" & src's quoted form & "; dst=" & dst's quoted form & "
  247. ct=$(stat -f '%SB' -t '%Y%m%d%H%M.%S' \"$src\")
  248. mt=$(stat -f '%Sm' -t '%Y%m%d%H%M.%S' \"$src\")
  249. touch -t $ct \"$dst\" # set creation date (<= current creation date)
  250. touch -mt $mt \"$dst\" # set modification date (>= creation date)"
  251. end inherit_file_dates

[CodeBlockEnd:17c5a62c-54d3-4061-ac41-37b7e4ed1ce1]



[CodeBlockStart:e2c458b8-4771-44db-8dab-e6447418de9f][excluded]


  1. --save_AW6_WP_as_Pages4.applescript
  2. (*
  3. Convert AppleWorks v6 WP (word processing) files to iWork'09's Pages v4 files
  4. v0.2
  5. v0.2 -
  6. changed the logging scheme from post-conversion log:
  7. converted x => y
  8. to pre-conversion log:
  9. converting x => y
  10. so that it can help to identify the last file which had caused the applicaton / script to crash.

  11. v0.1 -
  12. initial version
  13. * Tested with Pages 4.0.5 under OS X 10.6.8.
  14. * Specify property in_place in script as necessary.
  15. * Specify property inherit_dates in script as necessary.
  16. * A log file with time-stamp in name is created on desktop.
  17. * Original file name is always preserved except that name extension ".pages" is added
  18. Note that original name extension (.cwk) is NOT removed and thus, e.g., abc.cwk will be converted to abc.cwk.pages
  19. This is to avoid possible data loss in case there're file names, e.g., abc and abc.cwk in a source directory.
  20. *)
  21. _main()
  22. on _main()
  23. script o
  24. property in_place : true
  25. (*
  26. true : convert files in place, i.e. converted files are saved in original directories,
  27. where original source directory is archived in zip file with time-stamp in name in advance;
  28. false : converted files are saved under destination directory which is specified in a dialogue in script
  29. *)
  30. property inherit_dates : true
  31. (*
  32. true : destination file inherits creation date and modification date of source file
  33. false : destination file has creation date and modification date at the time of conversion
  34. *)
  35. property ext : ".pages"
  36. 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'")
  37. property pp : {}
  38. -- accept source directory (and destination direcotry if in_place is false)
  39. set srcd to (choose folder with prompt "Choose source directory where source files reside.")'s POSIX path
  40. if srcd ends with "/" and (count srcd) > 1 then set srcd to srcd's text 1 thru -2
  41. if not in_place then
  42. repeat
  43. set dstd to (choose folder with prompt "Choose destination directory where to save converted files.")'s POSIX path
  44. if dstd ends with "/" and (count dstd) > 1 then set dstd to dstd's text 1 thru -2
  45. if srcd ≠ dstd then exit repeat
  46. display dialog "Source and destination directories cannot be the same!" with icon stop
  47. end repeat
  48. end if
  49. log_printf(logfile, "conversion started in operation mode: in_place = %s, inherit_dates = %s.\\n", {in_place, inherit_dates})
  50. -- retrieve target files
  51. log_printf(logfile, "scanning files under %s\\n", srcd)
  52. set pp to _match(my is_AW_v6_WP, scan_AW_files(srcd))
  53. log_printf(logfile, "found %d file(s) to process.\\n", count my pp)
  54. -- process target files
  55. if (count my pp) > 0 then
  56. if in_place then
  57. -- archive the source directory first (zip file name = srcd_YYYY-MM-DDTHH.MM.SS.zip)
  58. set dst to do shell script "src=" & srcd's quoted form & "
  59. dst=\"${src}_$(date +'%FT%H.%M.%S').zip\"
  60. ditto -ck --keepParent --sequesterRsrc \"$src\" \"$dst\"
  61. echo \"$dst\""
  62. log_printf(logfile, "archived source directory in %s\\n", dst)
  63. repeat with p in my pp
  64. set p to p's contents
  65. set q to p & ext
  66. log_printf(logfile, "converting %s => %s\\n", {p, q})
  67. save_AW6WP_as_Pages4(p, q, {inherit_dates:inherit_dates})
  68. --do shell script "rm -f " & p's quoted form -- delete the source file [1]
  69. end repeat
  70. else
  71. set slen to count srcd
  72. repeat with p in my pp
  73. set p to p's contents
  74. set q to dstd & (p's text (slen + 1) thru -1) & ext
  75. log_printf(logfile, "converting %s => %s\\n", {p, q})
  76. save_AW6WP_as_Pages4(p, q, {inherit_dates:inherit_dates})
  77. end repeat
  78. end if
  79. end if
  80. -- completion notice
  81. log_printf(logfile, "process completed for total of %d file(s).\\n", count my pp)
  82. tell me
  83. activate
  84. display dialog "Done " & (count my pp) & " file(s)." giving up after 5 with icon note
  85. end tell
  86. (*
  87. [1] NOT recommended because conversion is not necessarily complete.
  88. *)
  89. end script
  90. tell o to run
  91. end _main

  92. on scan_AW_files(d)
  93. (*
  94. string d : POSIX path of source directory where to start scanning
  95. return list : list of POSIX paths of found files
  96. * query condition is (file name extension = "cwk") OR (file creator type = "BOBO")
  97. *)
  98. script o
  99. property pp : {}
  100. property qq : {}
  101. property rr : {}
  102. tell application "System Events"
  103. tell disk item d
  104. set pp to every folder's POSIX path
  105. repeat with p in my pp
  106. set qq to my scan_AW_files(p's contents)
  107. repeat with q in my qq
  108. set end of my rr to q's contents
  109. end repeat
  110. end repeat
  111. --set qq to every file's POSIX path whose name extension = "cwk" or (creator type = "BOBO" and file type = "CWWP")
  112. set qq to every file's POSIX path whose name extension = "cwk" or creator type = "BOBO"
  113. repeat with q in my qq
  114. set end of my rr to q's contents
  115. end repeat
  116. end tell
  117. end tell
  118. return my rr's contents
  119. end script
  120. tell o to run
  121. end scan_AW_files

  122. on is_AW_v6_WP(f)
  123. (*
  124. string f : POSIX path of source file
  125. return boolean : true if f is AW version 6 WP file, false otherwise
  126. *)
  127. (*
  128. byte[1] = version
  129. byte[5..8] = BOBO
  130. byte[279] (when byte[1] = 0x06) =
  131. 0 => draw
  132. 1 => word processing
  133. 2 => spreadsheet
  134. 3 => database
  135. 4 => paint
  136. 5 => presentation
  137. * byte index is 1-based
  138. *)
  139. set f to f as POSIX file
  140. (* -- 10.5 or later only
  141. return (read f from 5 for 4)'s id = {66, 79, 66, 79} and ¬
  142. (read f for 1)'s id = 6 and ¬
  143. (read f from 279 for 1)'s id = 1
  144. *)
  145. considering case
  146. return (read f from 5 for 4) = "BOBO" and ¬
  147. (ASCII number (read f for 1)) = 6 and ¬
  148. (ASCII number (read f from 279 for 1)) = 1
  149. end considering
  150. end is_AW_v6_WP

  151. on save_AW6WP_as_Pages4(src, dst, {inherit_dates:inherit_dates})
  152. (*
  153. string src : POSIX path of source file
  154. string dst : POSIX path of destination file
  155. boolean inherit_dates: true for dst to inherit creation and modification dates of src, false otherwise.
  156. * src may equate to dst, in which case src is overwritten
  157. * intermeditate directories in dst will be created as needed if not present
  158. *)
  159. -- get source and destination file
  160. set srcf to src as POSIX file
  161. set dstf to dst as POSIX file
  162. -- open srca in Pages v4 and save it in dstf
  163. tell application "Pages"
  164. --activate
  165. set k to count documents
  166. open srcf
  167. repeat until (count documents) > k
  168. delay 0.2
  169. end repeat
  170. tell document 1
  171. close saving in dstf
  172. end tell
  173. end tell
  174. -- wait for dst to come into existence
  175. wait_file_exist(dst)
  176. -- set label of destination file to label of source file -- [1]
  177. repeat 3 times -- max retry
  178. try -- [2]
  179. tell application "Finder"
  180. set lbl to item (srcf as alias)'s label index
  181. tell item (dstf as alias)
  182. update
  183. set its label index to lbl
  184. end tell
  185. end tell
  186. exit repeat -- exit if no error
  187. delay 0.5 -- wait some time before retry
  188. end try
  189. end repeat
  190. -- inherit creation and modication dates from src to tmp
  191. if inherit_dates then inherit_file_dates(src, dst)
  192. (*
  193. [1] This is required because Pages does not preserve the original label when saving file.
  194. [2] Finder is such an unreliable beast that it may fail even after the file indeed has come into existence.
  195. *)
  196. end save_AW6WP_as_Pages4

  197. on wait_file_exist(f)
  198. (*
  199. string f : POSIX path of file
  200. * wait until f comes into existence
  201. *)
  202. do shell script "f=" & f's quoted form & "
  203. until [[ -e \"$f\" ]]; do sleep 0.3; done"
  204. end wait_file_exist

  205. on _match(pat, aa)
  206. (*
  207. handler pat : handler to test elements in aa
  208. list aa : source list
  209. return list : list of every element a of list aa whose pat(a) = true
  210. *)
  211. script o
  212. property |::xx| : aa's contents
  213. property |::yy| : {}
  214. property |::f| : pat
  215. repeat with x in my |::xx|
  216. set x to x's contents
  217. if my |::f|(x) then set end of my |::yy| to x
  218. end repeat
  219. return my |::yy|'s contents
  220. end script
  221. tell o to run
  222. end _match

  223. on log_printf(f, fmt, lst)
  224. (*
  225. string f : POSIX path of log file
  226. string fmt : printf format string
  227. list lst : list of values (if lst is one item list {x}, lst may be x)
  228. * %-26s time-stamp in format %F %T%z is added to the beginning of each entry
  229. *)
  230. local args
  231. set args to "'%-26s'" & fmt's quoted form & " \"$(date +'%F %T%z')\" "
  232. repeat with a in {} & lst
  233. set args to args & (a as string)'s quoted form & space
  234. end repeat
  235. do shell script "printf " & args & " >> " & f's quoted form
  236. end log_printf

  237. on inherit_file_dates(src, dst)
  238. (*
  239. string src : POSIX path of source file
  240. string dst : POSIX path of destination file
  241. * If creation date of src is older than or equal to that of dst,
  242. this will set both creation date and modification date of dst to those of src.
  243. * If creation date of src is newer than that of dst,
  244. this will set only modification date of dst to that of src.
  245. *)
  246. do shell script "src=" & src's quoted form & "; dst=" & dst's quoted form & "
  247. ct=$(stat -f '%SB' -t '%Y%m%d%H%M.%S' \"$src\")
  248. mt=$(stat -f '%Sm' -t '%Y%m%d%H%M.%S' \"$src\")
  249. touch -t $ct \"$dst\" # set creation date (<= current creation date)
  250. touch -mt $mt \"$dst\" # set modification date (>= creation date)"
  251. end inherit_file_dates

Feb 17, 2015 2:35 PM in response to missknowitallsoshe thinks...

Hi All,

Thanks for all your help, esp Niel. i decided to copy and paste again directly from the web page, instead of my copy fro Text edit and IT WORKED!!! So in the end, i guess the problem was copying it into TE first and then copying THT and pasting into SE.

Who knew? Certainly not me, but I do now! Thanks again to all!!!

Joan

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to run a script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.