Le 29 oct. 2010 à 23:37, alma-services.abel.co.uk a écrit :
Hello Applescript users,
Let me explain where I'm coming from. I'm studying Mandarin Chinese, using the wonderful "Ni Hao" Chinese language course (by Paul and Shumang Fredlein, http://www.chinasoft.com.au/). To help with vocabulary, I use a program called StudyCard (by Terry Bird, http://www.digitalmeadow.com/). At first, I just entered more vocab into StudyCard, in little dribs and drabs, as I progressed through the chapters of the first book for Ni Hao. But after a while, it just became too many Chinese characters to easily enter and format (font size, card layout and colour).
So after a while I got intrigued by applescript. I messed around with it and came up with something that does the formatting of my study cards automatically, from a tab delimited RTF text file. My script is a bit flakey, and only fit for my own use right now. My problem is I don't know how to get the script to be user-friendly and open a file that the user chooses, from a dialogue. Instead, when the script runs, it assumes that the file is already open in Pages, and called "NiHao Raw.pages". Right now, everything falls over if it's not there when you click on run. What's the rick to getting a script to dialogue box and to choose and successfully open the (rtf) text document in Pages that's been selected?
So here's the whole script, below. (The big 'tell document "New NiHao"' block does the meat and potatoes of the formatting, and is irrelevant my choosing-an-input-file problem. I include it to give you the whole picture. And because I worked _really_ hard on getting the damn formatting right for doing my study cards on my little Palm Z22! Profesh...)
-- FILE STARTS: "NiHaoFormat.Test" --
local the_paragraphs, theParts set theParts to {}
-- closeAnnoyingDialog(application "Pages")
tell application "Pages" (* set theFile to (choose file with prompt "Choose the Ni Hao vocabulary file:" without invisibles) set fileName to name of (info for theFile) -- set theDocument to document fileName open theFile tell document fileName set the_paragraphs to paragraphs of body text end tell *) -- the "NiHao Raw.pages" input file should be open in Pages when running this script... tell document "NiHao Raw.pages" set the_paragraphs to paragraphs of body text end tell set n to count of the_paragraphs -- output file for you to rename and save... make new document at front with properties {name:"New NiHao", template name:"NiHao"} tell document "New NiHao" tell body text repeat with p from 1 to n set theParts to my split(item p of the_paragraphs, tab) set theCount to count theParts if theCount is greater than 0 then set theChineseCharacters to (item 1 of theParts) set countOfCharacters to (count theChineseCharacters) if countOfCharacters = 1 then make new text at end with data (theChineseCharacters) with properties {character style:"Ch1"} else if countOfCharacters = 2 then make new text at end with data (theChineseCharacters) with properties {character style:"Ch2"} else if countOfCharacters = 3 then make new text at end with data (theChineseCharacters) with properties {character style:"Ch3"} else if countOfCharacters = 4 then make new text at end with data (theChineseCharacters) with properties {character style:"Ch4"} else if countOfCharacters ≥ 5 then make new text at end with data (theChineseCharacters) with properties {character style:"Ch phrase"} else -- zero chars then don't do anything end if end if if theCount is greater than 1 then make new text at end with data (tab & item 2 of theParts) with properties {character style:"pinyin"} end if if theCount is greater than 2 then make new text at end with data (tab & item 3 of theParts) with properties {character style:"English Meaning"} end if if theCount is greater than 3 then make new text at end with data (" " & item 4 of theParts) with properties {character style:"pinyin-meaning"} end if -- not needed: make new text at end with data (return) end repeat end tell end tell end tell
on split(t, d) local L set AppleScript's text item delimiters to d set L to text items of t set AppleScript's text item delimiters to "" return L end split
(* closeAnnoyingDialog should close the annoying dialog that iWork 2008 * applications present when no document is open *) on closeAnnoyingDialog(theApplication) try set hasWindow to document of window 1 of theApplication hasWindow on error return end try try set firstWindowDoc to path of document of window 1 of theApplication firstWindowDoc on error tell theApplication to close window 1 end try end closeAnnoyingDialog
-- FILE ENDS "NiHaoFormat.Test" --
Sandy
set le_document to choose file of type {"com.apple.iwork.Pages.sffPages", "com.apple.iwork.Pages.Pages"} tell application "Pages" open le_document end tell
may be what you need.
A detail bothers me. In your main script I read :
repeat with p from 1 to n If my eyes did correctly their duty, the variable n isn't define before this instruction.
Yvan KOENIG (VALLAURIS, France) samedi 30 octobre 2010 22:42:55
|