Skip navigation

Here’s an AppleScript I use at work: its a file that when folders are dropped onto it, the images inside are opened up as an image sequence in QuickTime, made into movies, and then saved.

It it not really ready to be used by anyone (you’ll have to modify some paths, filenames, etc) but I just wanted to get this up and maybe I’ll fix it up later for more general purpose use.

This took me forever to make because I didn’t understand the QuickTime AppleScript Dictionary.

on open folderNames
	repeat with i from 1 to length of folderNames
		tell application "QuickTime Player"
			set compath to (POSIX path of item i of folderNames)
			(*tell application "Script Editor"
				display dialog compath
			end tell*)
			set documentObj to open image sequence (compath & "frame0.jpg") frames per second 30
			(**)
			set a to compath
			set e to (offset of "/" in a)
			set a to ((items (e + 1) thru ((((length of a)))) of a as text))
			set e to ""
			repeat while not e = ((length of a) + 1)
				set e to (offset of "/" in a)
				if e = ((length of a)) then
					exit repeat
				end if
				set a to ((items (e + 1) thru ((length of a)) of a as text))
			end repeat
			(**)
			set ending to a
			(* ending means folder name *)
			set basepath to (items (0 + 1) thru ((offset of (ending as text) in compath) - 1)) of compath as text
			set foldername to (items 1 thru ((length of ending) - 1)) of ending as text
			#tell application "Script Editor"
			#	display dialog foldername & ".mov"
			#end tell
			set (name of documentObj) to (foldername & ".mov")
			save self contained documentObj in (basepath & foldername & ".mov")
			#tell application "Script Editor"
			#	display dialog basepath & foldername & ".mov"
			# end tell
			close
		end tell
	end repeat
	display dialog "All Movies Saved - Quitting..."
	tell application "QuickTime Player"
		quit
	end tell
end open

http://koldfyre.wordpress.com/

http://pythonprog.wordpress.com/

http://iphoneaday.wordpress.com/

NEW – http://veryus.wordpress.com/
NEW – http://learnware.wordpress.com/

http://4pplescript.wordpress.com/

Readers, anyone, comment on what you want! What types of applescripts are you looking for, etc.
Anything related to AppleScript so I can put up some useful content!

Say you need to restrict your kid from doing something, like drawing in Photoshop. You could, of course, create separate accounts, password protect the admin account, restrict the account from opening Photoshop. … But that restricts Photoshop *forever*… How about just for 3 hours while she/he’s studying?

This is where this script comes in useful. This 3 line script will continually hide your application and if someone tries to open your application, they see a flicker as the application tries to show up but this script hides it so fast that you can’t see any of the content. Just replace the application name in the script with whatever you are trying to hide, run this script, then hide the script window.

After you have gotten back from your bathroom break, press Command + Alt/Option + ESC and Force Quit the script.

repeat
       tell application "System Events" to set visible of process "Firefox" to false
end repeat

Statements are a series of words that AppleScript recognizes and follows the correct syntax. There 3 main types of statements: control statement, simple statement, and a compound statement.


Control Statements


These statements determine the “how” and “when” of other statements/commands. Some common ones are if and repeat statements.

if myAge is greater than 10 then
     myName
endif

If the variable myAge is greater than 10, then AppleScript will print the value of myName in the “Result” box near the bottom of Script Editor. If something ends with end if.

repeat
     say "Hello"
end repeat

Notice that repeat also ends with an end repeat. Because no other information was given, this code snippet will repeat forever, and say “Hello” every time it repeats.

If you actually make a mistake like that in making an application, the best way to quit out would be to Quit but your computer may freeze up. In that case, press COMMAND + ALT + ESC (Command + Alt/Option + ESC). Then Force Quit your app.

A simple statement is simply a one-line statement and a compound statement is a multi-line statement that may contain other statements.

Simple Statement

tell application "Finder" to activate

This code tells Finder to become focused on, or if Finder is closed, to open up Finder. Applications have special names so Quicktime cannot just be referred to as “Quicktime”. It is actually “QuickTime Player” and it is easy to find out any applications name – just run the script with the wrong name and a dialog box will pop up asking you which application you meant to call.


AppleScript is Object Oriented


Object oriented is very popular now and seems to have replaced functional programming. If you do not know about object oriented programming, I would recommend you search it on Google and learn a little big more about it. It is definitely worth taking the time off and learning about. Google it!

Everything you work with in AppleScript is an object. The top-level object is the script object…I do not really understand either, so let’s work through some example code.

Pseudocode:

function calculateMean(a,b) {
      return (a + b)/2
}

Now when you call calculateMean(1,5) you should get back 3 (because 1+5 = 6, divided by 2 is 3). calculateMean() would be considered a function in functional programming and, essentially, a method in object-oriented programming. In AppleScript, calculateMean() is referred to as a handler. The variables a and b are parameters.

Legit AppleScript Code:
property myName : "Ed"

on sayName(nameVariable)
      display dialog ("Hi " & nameVariable)
end sayName

script testName
sayName(myName)
end script

run testName
sayName("Joe")

sayName is the handler. When you are defining a handler, the parameter you put in is just a variable that represents whatever you may put in there. So that parameter variable could have been any other variable name – it doesn’t matter. The ("Hi " & nameVariable) part is called string concatenation. It basically just adds/joins the strings together.

script testName seems to be a script inside a script (we call this nested script object(s) – and if inside a larger if statement is a nested if statement), that does not run unless we run it (as we did with the code, run testName). Notice the script testName ends with end script and not end testName.

And lastly, sayName("some string") is how to run a handler.

Repeat will loop forever unless given extra parameters (parameters is essentially extra instructions).
repeat
     -- code to repeat goes here
end repeat

You can break out of a repeat loop by using the command exit repeat coupled with an if ... then statement.

Example code of repeating a display dialog until some text is answered:

repeat
    display dialog "Enter name:" default answer ""
    set theName to text returned of result
    if not theName = "" then exit repeat
end repeat

Using Repeat Loops in AppleScript

Wondering what options are available for commands, such as display dialog? Check out Apple’s documentation.

Day 2. I already ditched that old chapter 3 link that I was using in Day 1 for the AppleScript Language Guide (PDF). This is going to be a run-through of all types of basic information you need to know in order to script in AppleScript.

And I’ll admit Day 1′s post was boring but basics are essential. Sorry about that.


Identifier


Identifier is basically a name for classes, variables, etc. It must begin with a letter and must be made from the following characters:
        ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_

Not case-sensitive so if myName is used first and then Myname is used, then while the script is compiling, Myname will be changed to myName.

Exception: Place vertical bars (| – the button above the enter key) around the identifier to contain any characters. Every other time you use/call the identifier, include the vertical bars.
Read More »

        The first step in learning any language/program is… Google. So I did a Google search and I decided to start with T&B’s AppleScript tutorial. I glanced through the Table of Contents (TOC) and figured starting at Chapter 3 would be good:
AppleScript Tutorial Chapter 3

OK, actually, I lied. The first thing you should do is see if you can get resources from the company that made the language/program:

Dictionary

AppleScript-ing is done in Script Editor (just search for it in Spotlight or QuickSilver) and is located in /Applications/AppleScript (go figure). So, AppleScript is very English-like and to know which words actually work, we need to open up a dictionary of whatever app we are trying use in our AppleScript. Read More »

Follow

Get every new post delivered to your Inbox.