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

 

Call Method in AppleScript

it ‚call method‘ ist es mit AppleScript möglich auf Cocoa Methoden zuzugreifen. Der direkte Zugriff war aus eine AS Studio Projekt möglich. Einfache AppleScripts können diese nur über einen kleinen Umweg mit ‚Automator‘ bewerkstelligen.

Trotz dem Ende von AS Studio mit AppleScriptObjC will ich einige Beispiele hier auflisten. Mit ASOC sind die Möglichkeiten natürlich noch umfangreicher um auf Cocoa Klassen zuzugreifen.

-- bouncing dock icon for short time
call method "requestUserAttention:" of class "NSApp" with parameter 10

-- endless bouncing dock icon
call method "requestUserAttention:" of class "NSApp" with parameter 1
-- Kapitalisieren (capitalize)
set foo to "The quick brown fox jumps over the lazy dog."
tell application "Automator"
  set capitalizedString to call method "capitalizedString" of foo
end tell
-- Alles Kleinbuchstaben (lowercase)
set foo to "The quick brown fox jumps over the lazy dog."
tell application "Automator"
  set lowercasedString to call method "lowercaseString" of foo
end tell
-- Alles Großbuchstaben (uppercase)
set foo to "The quick brown fox jumps over the lazy dog."
tell application "Automator"
  set uppercasedString to call method "uppercaseString" of foo
end tell
-- eine URL encoden
set foo to "http://www.spiegel.de/ööü"
tell application "Automator"
  set theNewPath to call method "stringByAddingPercentEscapesUsingEncoding:" of foo with parameter 5
  --> ergibt "http://www.spiegel.de/%E4%F6%FC"
end tell
-- eine URL decoden
tell application "Automator"
  set theNewestPath to call method "stringByReplacingPercentEscapesUsingEncoding:" of "http://www.spiegel.de/%E4%F6%FC" with parameter 5
  --> ergibt "http://www.spiegel.de/ööü"
end tell
-- Tilde (~) auflösen
set foo to "~/Library/Application Support"
tell application "Automator"
  set fullPathString to call method "stringByExpandingTildeInPath" of foo
end tell
-- Tilde (~) wieder hineinrechnen
set foo to POSIX path of (path to desktop folder)
tell application "Automator"
  set pathStringWithTilde to call method "stringByAbbreviatingWithTildeInPath" of foo
end tell
-- Parent-Folder holen
set foo to "~/Library/Application Support"
tell application "Automator"
   set parentPath to call method "stringByDeletingLastPathComponent" of foo
end tell
-- Pfad um Komponente erweitern
set foo to "~/Library/"
tell application "Automator"
  set deeperPath to call method "stringByAppendingPathComponent:" of foo with parameters {"Application Support"}
end tell
-- Pfadkomonenten als Liste holen
set foo to "~/Library/Application Support"
tell application "Automator"
  set foo to call method "stringByExpandingTildeInPath" of foo
  set pathComponentsList to call method "pathComponents" of foo
end tell
-- Mehrere Pfade gleichzeitig bauen
set foo to "/Applications"
set fooList to {"Utilities/Terminal.app", "iTunes.app", "Dictionary.app"}
tell application "Automator"
  set pathList to call method "stringsByAppendingPaths:" of foo with parameters {fooList}
end tell
-- einen kompletten Pfad bauen
set fooList to {"Applications", "Utilities", "Terminal.app"}
tell application "Automator"
  set theNewPath to call method "pathWithComponents:" of class "NSString" with parameters {fooList}
end tell
--search & replace in string
set {sourcestring, searchstring, replacestring} to {"Ich will ersetzt werden ;-)", "ersetzt", "versetzt"}
tell application "Automator"
  set resultstring to call method "stringByReplacingOccurrencesOfString:withString:" of sourcestring with parameters {searchstring, replacestring}
end tell
--php like explode/implode
set {sourcestring, delimiter} to {"Das#ist#ein#Test", "#"}
tell application "Automator"
  set splitStringList to call method "componentsSeparatedByString:" of sourcestring with parameter delimiter
end tell
--php like explode/implode
set {sourcestring, delimiter} to {"Das#ist#ein#Test", "#"}
tell application "Automator"
  set splitStringList to call method "componentsSeparatedByString:" of sourcestring with parameter delimiter
  set joinedString to call method "componentsJoinedByString:" of splitStringList with parameter delimiter
end tell

 

Monkey: basic app structure

 

Strict

Import mojo

Function Main:Int()
  New MyGame()
  Return 0
End Function

Class MyGame Extends App

  'Called once, do your initialization here!
  Method OnCreate:Int()
    SetUpdateRate(30)
    Return 0
  End Method

  'Called 30 times per second (normally)
  Method OnUpdate:Int()
    Return 0
  End Method

  'This is called when the user pressed the home button!
  Method OnSuspend:Int()
    Return 0
  End Method

  'This is called when the user switches back to your app and it's still alive!
  Method OnResume:Int()
    Return 0
  End Method

  'This is called when something is rendered to the screen
  Method OnRender:Int()
    Cls()
    DrawText( "Hello there", 10, 10 )
    Return 0
  End Method

  'This is called when the back button was pressed!
  Method OnBack:Int()
    'Ok, just exit the app for test purposes!
    EndApp()
    Return 0
  End Method
End Class

Objective-C: resizing an UIImage

-(UIImage*)imageWithImage:(UIImage*)image 
                 scaledToSize:(CGSize)newSize;
{
 UIGraphicsBeginImageContext( newSize );
 [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
 UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 return newImage;
}

 

// grab the original image
UIImage *originalImage = [UIImage imageNamed:@"myImage.png"];
// scaling set to 2.0 makes the image 1/2 the size. 
UIImage *scaledImage = [UIImage imageWithCGImage:[originalImage CGImage] scale:2.0 orientation:UIImageOrientationUp];

 

Test AppleScript

--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