Mehrsprachigkeit in AppleScript Studio

In einem AppleScript Studio Projekt kann die Mehrsprachigkeit über eine Localizable.strings Datei berücksichtigt werden. Ein Blick in die studioreference.pdf dürfte hilfreich sein.

Standardsprache ist english. In der jeweiligen Landessprache können Übersetzungen wie folgt angelegt werden:

English.lproj/Localizable.strings:

/* Text for the Open button */

„OPEN_KEY“ = „Open“;

/* Text for the Close button */

„CLOSE_KEY“ = „Close“;

 

French.lproj/Localizable.strings:

/* Text for the Open button */

„OPEN_KEY“ = „ouvrez-vouz“;

/* Text for the Close button */

„CLOSE_KEY“ = „étroit“;

Der Zugriff auf die Sprachdatei erfolgt über folgenden Befehl:

set translatedString to localized string "OPEN_KEY" from table "Localizable"

 

Java und AppleScript

Tja, AppleScript in Verbindung mit Java waren bzw. sind in meine Augen ein sehr gutes Gespann. Apple bot extra für diese Zwecke eine Cocoa-Java Bridge an über die man auch auf AppleScript zugreifen konnte.

Doch wer Apple kennt weiß das die Firmenpolitik strikt ist und konsequent ein Schlußstrich gezogen wird, wenn Technologien in Apples Augen veraltet sind oder nicht mehr gebraucht werden. So erging es auch der Cocoa-Java Bridge und diese wurde seit Mac OS X Tiger 10.4 nicht mehr weiter unterstützt.

In Java wurde aber eine Java Scripting API integriert mit der man auch unter anderem wieder auf AppleScript zugreifen kann. Diese nutzt auch der Java-AppleScript-Connector der über einen JNI Wrapper das AppleScript über „osacompile“ in der Kommandozeile ausführt.

Anhand eines kurzen Beispiels mit der Netbeans IDE zeige ich wie man auf AppleScript zugreifen kann.

Zuerst fügen wir die jasconn.jar in den Libraries des neu angelegten Netbeans Projekts hinzu.

Im Netbeans  GUI Editor fügen wir einen Button hinzu der eine Methode ausführt in die wir folgenden kleinen Beispielcode einfügen.
public void callAppleScript() {
      ScriptEngineManager sem = new ScriptEngineManager();
      ScriptEngine e = sem.getEngineByName("jasconn");
      //ScriptEngine e = sem.getEngineByName("AppleScript");
      ScriptEngineFactory f = e.getFactory();
      String[] statements = {
          "tell application \"QuarkXPress Passport\" to set x to get properties of every generic box of document 1",
          "return x",
      };
      String program = f.getProgram(statements);
      try {
          //Rückgabewert
          Object resultOfAppleScript = e.eval(program);
          System.out.println("Result:" + resultOfAppleScript.toString());
      } catch (ScriptException ex) {
          ex.printStackTrace();
      }
  }

Nun die Java App kompilieren auf den Button klicken. Das AppleScript wird ausgeführt und der Rückgabewert wird übergeben und zusötzlich über println() ausgegeben.

Im Beispiel weisen wir QuarkXPress Passport an die Eigenschaften aller generic boxes auszugeben.

 

Image Events mit AppleScript

-- save in Script Editor as an Application, then
-- drag image files onto the Application's icon
on open some_items
    repeat with this_item in some_items
        try
            rescale_and_save(this_item)
        end try
    end repeat
end open

to rescale_and_save(this_item)
    tell application "Image Events"
        launch
        set the target_width to 120
        -- open the image file
        set this_image to open this_item
        set typ to file type of this_image
        copy dimensions of this_image to {current_width, current_height}
        if current_width is greater than current_height then
            scale this_image to size target_width
        else
            -- figure out new height
            -- y2 = (y1 * x2) / x1
            set the new_height to (current_height * target_width) / current_width
            scale this_image to size new_height
        end if

        tell application "Finder" to set new_item to (container of this_item as string) & "scaled." & (name of this_item)
        save this_image in new_item as typ
    end tell
end rescale_and_save
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "JPEG", "JPEG2", "jpg", "public.jpeg", ¬
    "PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}

--Get the artwork file
set theFiles to choose file with prompt "Choose art file(s)" of type openTypes with multiple selections allowed without invisibles
runConversion(theFiles)

on open someFiles
    runConversion(someFiles)
end open

on runConversion(theItems)
    set saveFolder to choose folder with prompt "Save resized pictures where?" without multiple selections allowed and invisibles
    tell application "Image Events"
        launch
        set newHeight to 600
        set newWidth to 0
        if (count items of theItems) is greater than 0 then
            repeat with anItem in theItems
                set imageFile to (open anItem)
                set theSize to dimensions of imageFile
                set width to item 1 of theSize
                set height to item 2 of theSize
                set ratio to (width / height)
                set newWidth to (ratio * newHeight) as integer
                if newHeight > newWidth then
                    scale imageFile to size newHeight
                else
                    scale imageFile to size newWidth
                end if
                save imageFile as JPEG in saveFolder
                close imageFile
            end repeat
        else
            display dialog "Nothing to convert."
        end if
    end tell
end runConversion
global theImage

-- Prompt the user to select a file
set theImage to choose file with prompt "Please select an image file:" without invisibles
-- Prompt the user to enter the width of the picture
set theResult to display dialog "Saisissez la largeur de l'image." default answer "640"
set theAnswer to text returned of theResult

-- Retrieve the resize percentage amount
set theNewWidth to theAnswer as integer

-- Determine a path in which to save the resized image
tell application "Finder"
    set theImageName to name of theImage
    set theImageFolder to (folder of theImage) as string
end tell
set theResizedImagePath to theImageFolder & "Resized-" & theImageName as string

-- Open the image, resize it, and save it as a new file
tell application "Image Events"
    launch
    set theImage to open theImage

    -- This is the tricky part: you need to keep the ratio of the original picture
    copy dimensions of theImage to {currentWidth, currentHeight}
    set ratio to theNewWidth / currentWidth

    tell theImage
        scale by factor ratio to size theNewWidth
        save in theResizedImagePath
        close
    end tell
end tell
set this_file to choose file

try
    tell application "Image Events"
        launch
        set this_image to open this_file
        scale this_image to size 318
        rotate this_image to angle 45
        pad this_image to dimensions {800, 600}
        save this_image with icon
        close this_image
    end tell
on error error_message
    display dialog error_message
end try

 

 

Lade Datei in AppleScript Variable

--example 1
set theFile to (choose file with prompt "Select a file to read:" of type {"TEXT"})
open for access theFile
set fileContents to (read theFile)
close access theFile

--example 2
on readFile( unixPath )
    set foo to (open for access (POSIX file unixPath ))
    set txt to (read foo for (get eof foo))
    close access foo
    return txt
end readFile

--example 3 UTF-16
on readFile( unixPath )
    set foo to (open for access (POSIX file unixPath ))
    set txt to (read foo for (get eof foo) as Unicode text)
    close access foo
    return txt
end readFile

--example 4 UTF-8
on readFile( unixPath )
    set foo to (open for access (POSIX file unixPath ))
    set txt to (read foo for (get eof foo) as «class utf8»)
    close access foo
    return txt
end readFile

--example 5 with terminal
on readFile( unixPath )
    return (do shell script "cat '" & unixPath & "'")
end