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


  • 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(
	at Method)
	at java.lang.ClassLoader.loadClass(
	at sun.misc.Launcher$AppClassLoader.loadClass(
	at java.lang.ClassLoader.loadClass(
	at java.lang.ClassLoader.loadClassInternal(


Tips and tricks

HOWTO: Get the String length

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


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


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


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


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


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 (\).


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


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