missknowitallsoshe thinks...

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

Posted on Feb 17, 2015 11:44 AM

Close

Q: How to run a script

  • All replies
  • Helpful answers

  • by Niel,

    Niel Niel Feb 17, 2015 11:46 AM in response to missknowitallsoshe thinks...
    Level 10 (311,951 points)
    Feb 17, 2015 11:46 AM in response to missknowitallsoshe thinks...

    Copy the text, paste it into the Script Editor, and click Run. The 'tell' statement is only needed if the script interacts with an application.

     

    (122555)

  • by missknowitallsoshe thinks...,

    missknowitallsoshe thinks... missknowitallsoshe thinks... Feb 17, 2015 11:52 AM in response to Niel
    Level 1 (0 points)
    Feb 17, 2015 11:52 AM in response to Niel

    Thanks, Neil! I'll try it in a few minutes... Stay tuned! 

  • by missknowitallsoshe thinks...,

    missknowitallsoshe thinks... missknowitallsoshe thinks... Feb 17, 2015 12:03 PM in response to missknowitallsoshe thinks...
    Level 1 (0 points)
    Feb 17, 2015 12:03 PM in response to missknowitallsoshe thinks...

    So I hit run, and i got this message. now what do I do?

    Screen shot 2015-02-17 at 2.59.29 PM.png

  • by Niel,

    Niel Niel Feb 17, 2015 12:11 PM in response to missknowitallsoshe thinks...
    Level 10 (311,951 points)
    Feb 17, 2015 12:11 PM in response to missknowitallsoshe thinks...

    Delete all of the line numbers.

     

    (122559)

  • by missknowitallsoshe thinks...,

    missknowitallsoshe thinks... missknowitallsoshe thinks... Feb 17, 2015 12:52 PM in response to missknowitallsoshe thinks...
    Level 1 (0 points)
    Feb 17, 2015 12:52 PM in response to missknowitallsoshe thinks...

    Thanks again, Niel. Did that and now this:

     

    Screen shot 2015-02-17 at 3.50.42 PM.png

  • by missknowitallsoshe thinks...,

    missknowitallsoshe thinks... missknowitallsoshe thinks... Feb 17, 2015 12:56 PM in response to missknowitallsoshe thinks...
    Level 1 (0 points)
    Feb 17, 2015 12:56 PM in response to missknowitallsoshe thinks...

    Or this:

    Screen shot 2015-02-17 at 3.55.00 PM.png

  • by Niel,

    Niel Niel Feb 17, 2015 1:37 PM in response to missknowitallsoshe thinks...
    Level 10 (311,951 points)
    Feb 17, 2015 1:37 PM in response to missknowitallsoshe thinks...

    Delete all of the text preceding 'property' in that line.

     

    (122567)

  • by missknowitallsoshe thinks...,

    missknowitallsoshe thinks... missknowitallsoshe thinks... Feb 17, 2015 1:49 PM in response to missknowitallsoshe thinks...
    Level 1 (0 points)
    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...

    Screen shot 2015-02-17 at 4.45.39 PM.png

  • by Niel,

    Niel Niel Feb 17, 2015 1:51 PM in response to missknowitallsoshe thinks...
    Level 10 (311,951 points)
    Feb 17, 2015 1:51 PM in response to missknowitallsoshe thinks...

    Put a space after property. If you have further trouble with it, please copy and paste the script text here instead of taking a screenshot.

     

    (122571)

  • by Roger Wilmut1,

    Roger Wilmut1 Roger Wilmut1 Feb 17, 2015 2:00 PM in response to missknowitallsoshe thinks...
    Level 9 (77,925 points)
    iTunes
    Feb 17, 2015 2:00 PM in response to missknowitallsoshe thinks...

    That line should read what it did in the first place, without the spaces at the beginning -

     

    property in_place : true

     

    (you need the spaces after property and in_place)

  • by VikingOSX,

    VikingOSX VikingOSX Feb 17, 2015 2:23 PM in response to missknowitallsoshe thinks...
    Level 7 (20,618 points)
    Mac OS X
    Feb 17, 2015 2:23 PM in response to missknowitallsoshe thinks...

    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 a v0.2 with fixes here.

  • by VikingOSX,

    VikingOSX VikingOSX Feb 17, 2015 2:27 PM in response to missknowitallsoshe thinks...
    Level 7 (20,618 points)
    Mac OS X
    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.

  • by missknowitallsoshe thinks...,

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

    missknowitallsoshe thinks... missknowitallsoshe thinks... Feb 17, 2015 2:35 PM in response to missknowitallsoshe thinks...
    Level 1 (0 points)
    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