Monday, October 24, 2016

#1 2007-01-28 07:19:11 am

Pineapple
Member
Registered: 2007-01-28
Posts: 2

Converting A File From AppleWorks To Pages

Hello

I have a programming background but am completely new to Applescript.  I use Apple’s Pages program from the iWork suite a log.  It doesn’t come with any useful Automator actions and my goal is to write some using Applescript.  The first script I want to make is to convert a bunch of Appleworks files into Pages documents.  To do that I via the OS X GUI I would have to open the appleworks file with Pages and save it with a new filename.

So I’ve written the script below and saved it as an application.  It nearly works.  The idea is that I can drag a file or folder to the icon and it does the conversion for me.

Applescript:

on open filenames
   repeat with f in filenames
       tell application "Pages"
           open f
           close front window saving in my pages_filename(f)
       end tell
   end repeat
end open

on pages_filename(f)
   set this_item to the info for f
   set the file_name to the name of this_item
   set file_extension to the name extension of this_item
   if the file_extension is "" then
       set the trimmed_name to the file_name
   else
       set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
   end if
   set target_name to (the trimmed_name & ".pages") as string
   return the target_name as string
end pages_filename

1. The first problem I have is with my pages_filename function. It doesn’t return a filename with the full path of the original file e.g. HD:folder:folder:filename.pages.
I don’t know how to get the original path off the filename so I can concatenate it to the new filename.  Please could someone tell me how?

2. How do you look up what you can do with an object of a particular type for example ‘to the name of’ and ‘to the name extension of’  in the above fragments?

3. There is absolutely no error handling in here at the moment, what should I build in?

4. Am I going about things in the right way to achieve my longer term goal of turning this into an automator action.

Thanks

P


Filed under: pages

Offline

 

#2 2007-01-28 01:40:25 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2649

Re: Converting A File From AppleWorks To Pages

Welcome to AppleScript, Pineapple. smile

Pineapple wrote:

1. The first problem I have is with my pages_filename function. It doesn’t return a filename with the full path of the original file e.g. HD:folder:folder:filename.pages.
I don’t know how to get the original path off the filename so I can concatenate it to the new filename.  Please could someone tell me how?

Here's one possibility:

Applescript:

on parentFolder for someItem
   -- `someItem` should be an alias, a file specification, or text
   set someItem to "" & someItem -- Coerce to text
   
   set ASTID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to ":"
   try
       set someItem to text 1 thru text item -2 of someItem
   end try
   set AppleScript's text item delimiters to ASTID
   
   return someItem
end parentFolder

Pineapple wrote:

2. How do you look up what you can do with an object of a particular type for example ‘to the name of’ and ‘to the name extension of’  in the above fragments?

Well, in your example, you're getting that information from the `info for` command, which is part of Standard Additions (a scripting addition, or OSAX). If you go to Script Editor's File meun and choose "Open Dictionary…", then you will be shown a list of scriptable applications and scripting additions. Choose Standard Additions from the list, and you will see (and be able to search through) quite a few useful commands.

Side note: `info for` returns a record, which is conceptually comparable (there are some limitations) to associative arrays in other languages.

Side note: Here's another way of getting the name without an extension (thanks to Adam Bell for this one):

Applescript:

on nameWithoutExtension for someAlias
   -- `someAlias` should be an alias or file specification
   tell (info for someAlias) to return text 1 thru ((my (offset of ("." & name extension & ":") in (name & ":"))) - 1) of name
end nameWithoutExtension

3. There is absolutely no error handling in here at the moment, what should I build in?

For repeat loops, I prefer to use something like this:

Applescript:

repeat with thisItem in theseItems
   try
       -- do something
   on error errMsg number errNum
       display dialog "Error " & return & return & errMsg with icon caution buttons {"Cancel Script", "Skip Item"} default button 2
       if button returned of result is "Cancel Script" then error number -128 -- cancel
   end try
end repeat

Side note: If your script will only be running on Mac OS X v10.4 or later, you can use the better looking `display alert` command:

Applescript:

repeat with thisItem in theseItems
   try
       -- do something
   on error errMsg number errNum
       display alert "Error " & errNum message errMsg as warning buttons {"Cancel Script", "Skip Item"} cancel button 1 default button 2
   end try
end repeat

See also: Try Statements

Offline

 

#3 2007-01-29 03:04:30 pm

Pineapple
Member
Registered: 2007-01-28
Posts: 2

Re: Converting A File From AppleWorks To Pages

Thanks for your answers Bruce!  Most appreciated.

P

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.082 seconds, 9 queries executed ]

RSS (new topics) RSS (active topics)