AppleScript

Why this page? I want to script some iTunes commands, but I do not know AppleScript. I want to parse the comment field of the songs I have. I need to know how to work with strings.

Java is the main programming language I'm using. I'm used to having String operations available like: length, substring, indexOf, lastIndexOf, replace, split, startsWith… etc. However AppleScript does not have these String operations available out of the box.

Java and Applescript

So here I collect some information I will probably need to use to handle my comment string…

Perhaps I should not even use AppleScript but Java instead…

Or should I learn OS X Cocoa programming?

But it also requires AppleScript calls to iTunes…

Java issues

Using the cocoa libraries as described in http://macdevelopertips.com/java/netbeans-java-applescript-and-itunes-part-2.html is an easy way to let java execute applescript commands. However the cocoa libraries are deprecated since OSX 10.4

I tried using them on OS X 10.5, and they do work, but how long will they keep working?

Options

  • Use deprecated cocoa lib
  • Use java.exec to call some commandline scripts and create my own wrappers
  • Use some kind of AppleScript - Java bridge that does not depend on cocoa java libs.

Deprecated cocoa lib

  • It is one of the easiest solutions
  • It is deprecated, so will it still work with the next OS X update??

Java.exec to call commandline

  • This can work
  • Affraid of reinventing the wheel again and again

AppleScript - Java Bridge

I found an open source bridge framework on

However this was not for Java, but it is quite elegant to have a language specific bridge to applescript on which I only would need to write some iTunes code.

This made me look further for a nice java bridge and I ran into:

Jasconn requires java 1.6

On OS X java 1.6 JRE is available but it is not enabled by default. You can install it via the software update option if you not already have done so.

You can enable it as follows:

In short:

  • /Applications/Utilities/Java/Java Preferences.App
  • Move 1.6 to the top
  • Test in console with “java - version”

If you don't enable 1.6 you can run into:

java -jar jasconn.jar 
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)

Links

Tips and tricks

HOWTO: Get the String length

set theText to "blah"
theText's length --> 4
count theText --> 4

Source: http://bbs.macscripter.net/viewtopic.php?id=24558

How can I find specific characters in a text? (eg, numbers, capitals)

stripNumbers("I'm the number 1.") --> "I'm the number ."

to stripNumbers(inputText)
   set theNumbers to "0123456789"
   
   set prevTids to AppleScript's text item delimiters
   repeat with i in theNumbers
       set AppleScript's text item delimiters to i
       set inputText to inputText's text items
       set AppleScript's text item delimiters to prevTids
       set inputText to inputText as text
   end repeat
   
   inputText
end stripNumbers

Source: http://bbs.macscripter.net/viewtopic.php?id=24532

How do I concatenate text in AppleScript?

set x to "a" & "b" --> "ab"
set x to "a" & {"b", 5} --> "ab5"
set x to 5 & "a" --> {5, "a"}

Source: http://bbs.macscripter.net/viewtopic.php?id=24531

How can I know if a specified string is contained in a text?

set theText to "this is a sample"
set lookingFor to "sample"

offset of lookingFor in theText --> if result is not "0", then it is contained
theText contains lookingFor --> true or false, by default ignores case
lookingFor is in contents of theText --> the same

Source: http://bbs.macscripter.net/viewtopic.php?id=24519

How can I make a search/replace operation in a given text?

searchReplace("Life is pink", "pink", "black") --> "Life is black"

to searchReplace(thisText, searchTerm, replacement)
   set AppleScript's text item delimiters to searchTerm
   set thisText to thisText's text items
   set AppleScript's text item delimiters to replacement
   set thisText to "" & thisText
   set AppleScript's text item delimiters to {""}
   return thisText
end searchReplace

Source: http://bbs.macscripter.net/viewtopic.php?id=24515

How can I create a string containing a quote or a backslash?

set theString to "\"This is a sentence wraped in quotes.\""
--> "This is a sentence wraped in quotes."

set theString to "This is a sentence containing a backslash (\\)."
--> This is a sentence containing a backslash (\).

Source: http://bbs.macscripter.net/viewtopic.php?id=24502

How can I split a string?

split("This is a text", space)
--> {"This", "is", "a", "text"}

to split(someText, delimiter)
   set AppleScript's text item delimiters to delimiter
   set someText to someText's text items
   set AppleScript's text item delimiters to {""} --> restore delimiters to default value
   return someText
end split

Source: http://bbs.macscripter.net/viewtopic.php?id=24473

apple/applescript.txt · Last modified: 2012/04/14 15:13 (external edit)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki