# Where can I learn Applescript on the Internet for free?



## eliezer (Oct 20, 2005)

Is there a list on the internet with all the Applescript commands and what they do?

Also is there a web site where i can learn applescript for free? something like w3schools website but for applescript.


thanks


----------



## Mikuro (Oct 20, 2005)

Check out http://www.apple.com/macosx/features/applescript/resources.html

Specifically, download Apple's own AppleScript Language Guide, and also their Scripting Additions guide.

Once you get a basic grip on the syntax, which shouldn't take very long, just drag some apps onto Script Editor and see what their scripting dictionaries have to offer. (The Finder is located at "/System/Library/CoreServices/Finder.app", in case you don't know.)


----------



## eliezer (Oct 20, 2005)

ok. its going to be impossible for me to do what i want.

basically i want to make a script so that when i drag a file (some sort of movie) onto the icon, it converts that file to .mov format using ffmpegX.

then i want to make another script that will convert .mov files to .mp4 files, using quicktime pro, with specific export settings (e.g. 176x144, frame rate=15, data rate=150kbit/sec, etc.)

am i asking for way to much here and this script will be impossible to make?

please reply with what you think about this. thanks


----------



## Mikuro (Oct 20, 2005)

Wellll.....after some experimenting with ffmpegX, I would say that YES, this would be possible. ffmpegX does not have a very elegant scripting library  in fact, it looks like it's 100% generic user interface accessors  but it ought to be enough to do what you need.

What I would do is, first of all, create a preset in ffmpegX and save it (File menu). Then the first thing you do in your script is open that file. Then you'd do something like this to tell ffmpegX what file to convert and where to save it:

```
tell application "ffmpegX"
	tell the content view of window 1
		set the string value of text field 5 to "/Location/To/The/File/To/Convert.avi"
		set the string value of text field 2 to "/Location/To/Save/Result.mov"
		perform action button "Encode"
	end tell
end tell
```
Launch ffmpegX and try running that script. You'll see that it'll insert that text into the source and destination fields, just as if you'd typed it in manually. Then it'll click the Encode button which will get it all started.

QuickTIme Player has a pretty good scripting library, so I think that part should be easy enough. I know QuickTime has scripting commands for exporting movies, but I've never used them myself, so I can't say for sure if they're flexible enough.


----------



## eliezer (Oct 21, 2005)

ok thanks. but when i run the script i get an error message saying: "NSReceiverEvaluationScriptError: 4". I think this is because the first screen that comes has 3 options: "Register", "Enter Code" and "Try for Free". How do I make it automatically select "Try for free"?

I tried:
[perform action button "Try for free"]
but it didn't work.

What should I put in?


----------



## Mikuro (Oct 21, 2005)

Ahhh, yeah. I'm not sure why you can't say [button "Try for free"] here, but it gives me the same error. Don't worry, there are other ways to refer to buttons. But before I tell you how to do it, let me explain how I figure this stuff out to begin with.

The first step is to open ffmpegX and just leave the window you want to manipulate open. Then go to Script Editor and start messing around. Just about every application lets you access "window x", where x is a number. The active window is window 1. So the first thing to do is "get window 1". This takes window 1 and stores it, so you can refer to it later as "the result". (Alternatively, you can just say "window 1" everywhere, but this method becomes a lot simpler when you're dealing with things like "group 1 of tab group 1 of window 1", so it's a good idea to get comfortable with it.)

Now you want to see what "the result" (window 1) has to offer. It's a good idea to look in the scripting dictionary of any app you're dealing with. So drag ffmpegX from the Finder onto the Script Editor icon, and look for the "Window" class (it's under the "Application Suite" section).

There you'll see, right at the top, a list of elements it contains. Boxes, browsers, buttons....buttons! That's what we want. (We already knew that, but now you know where to look for this stuff.)

So, let's say [return every button of the result]. You'll get a bunch of cryptic descriptions of buttons like _button id 29 of window id 7 of application "ffmpegX"_. So which one is the 'Try for free' button? To find that out, replace this line with [return *the title of* every button of the result]. That returns a much easier to read list: {"Try for free", "Enter code", "Register"}. 'Try for free' is the first button in that list. That means we can refer to it as "button 1".

SO, cut out that line we just made, and replace it with [perform action *button 1 of the result*]. And there ya go.

You should also add an IF statement to be sure "window 1" is the "Please register" window. After all, it's possible you'll run this script after you've already loaded ffmpegX, in which case that window won't be open. That's easy enough. So, now, the complete script to dismiss this window:

```
tell application "ffmpegX"
	if the title of window 1 is "Please register" then
		perform action button 1 of window 1
	end if
end tell
```


----------



## eliezer (Oct 22, 2005)

thanks a lot for your help. i really appreciate it.

this is what i have so far:


```
tell application "ffmpegX"
	if the title of window 1 is "Please register" then
		perform action button 1 of window 1
	end if
	tell the content view of window 1
		set the string value of text field 5 to "/Location/To/The/File/To/Convert.avi"
		set the string value of text field 2 to "/Users/admin/Desktop/XYZ.mov"
		perform action button "Encode"
	end tell
end tell
```

What i want to do next is:
1) Make the file to be converted be the file that i drop onto the script icon.
2) Tell ffmpegX the settings for the target format. (I know you told me create a Preset format but i had trouble loading it)
3) I need to do something about what the file will be saved as. I want the file to be saved to the desktop, but I don't want the file to be saved as XYZ.mov. I want each file to be saved as something else.

If you don't know how to do this stuff, or if it's not possible, it's not a problem, you've helped me enough already. thanks


Also, i didn't quite understand this bit:

So, let's say [return every button of the result]. You'll get a bunch of cryptic descriptions of buttons like button id 29 of window id 7 of application "ffmpegX". So which one is the 'Try for free' button? To find that out, replace this line with [return the title of every button of the result]. That returns a much easier to read list: {"Try for free", "Enter code", "Register"}. 'Try for free' is the first button in that list. That means we can refer to it as "button 1".​
Where do i type in the stuff in the square brackets?


Thanks Mikuro


----------



## Mikuro (Oct 23, 2005)

I just realized, there's actually a MUCH better way of getting the source file into ffmpegX. We don't need to be messing around with the text field at all. All we have to do is tell the FINDER to open our source movie _with ffmpegX_. That'll make things a lot easier. So replace all that "set the string value of text field 5" business with this new method, which I'll detail in a bit.



> Where do i type in the stuff in the square brackets?


Those particular lines need to be after the "get window 1". So all in all it would look something like this:
	
	



```
tell application "ffmpegX"
	get window 1
	return the title of every button of the result
end tell
```



> 1) Make the file to be converted be the file that i drop onto the script icon.


To make a droplet, there are two things you need to do:
1) Create an "on open {the_file}" statement, where the_file is the variable that will store the file that was dropped.
2) Save the script as an application.

Once we have the_file, we just open it with ffmpegX, like I mentioned before.
	
	



```
tell application "Finder"
	open (the_file) using application file ((the application file of application process "ffmpegX") as text)
end tell
```
I know that whole [_application file ((the application file of application process "ffmpegX") as text)_] looks nasty, and it is. I'm not sure WHY we have to coerce it to text and then back to 'application file', but...we do. Whatever works, right? 

Another thing I should mention is that the "open" handler will only process ONE file. If you drag a bunch of files onto your icon, it won't process them all. There are some nasty ways around this, though. Take a look at some of Apple's examples, like "/Library/Scripts/ColorSync/Build profile info web page".



> 2) Tell ffmpegX the settings for the target format. (I know you told me create a Preset format but i had trouble loading it)


I'm getting problems myself now, too. Ouch. I have NO problems when opening my presets manually. I'm not sure what the script is doing differently, but I think that's what we need to figure out.

You COULD enter all your settings by manipulating the buttons and stuff, the same way we dismissed the registration window and entered the file paths. Follow the general procedure I explained before, and you should be able to manually configure it. Buuuut, it'd be a big fat pain in the rear, to say the least. It takes a lot of trial-and-error poking around to get that kind of thing done, and ffmpegX has a LOT of interface elements to contend with. I'm scared just thinking about it.

I'm going to mess around with this later. There's gotta be a way to get it working. The code I have so far (which doesn't work sometimes) is this:
	
	



```
tell application "Finder"
	open file "Path:to:preset.ffx"
	open (the_file) using application file ((the application file of application process "ffmpegX") as text)
end tell
```
Edit: I just flipped those two lines, so that it'll load the preset AFTER it loads the movie, and now it seems to work. Does that work on your end, too?



> 3) I need to do something about what the file will be saved as. I want the file to be saved to the desktop, but I don't want the file to be saved as XYZ.mov. I want each file to be saved as something else.


That's doable, too. It just depends on how you want to do it.

Do you want to use a standard Save dialog? If so, what you need is in Standard Additions, an AppleScript user's best friend. It's located at "/System/Library/ScriptingAdditions/StandardAdditions.osax". Drag it onto Script Editor and peek inside. In the "User interaction" section, you'll find everything you need. To get a standard Save dialog, use the "choose file name" function.

Also, using the new method I mentioned of loading the movie into ffmpegX, we'll get ffmpegX's automatic destination generation, which will be in the same folder as the source. If that's good enough for you, it'd save a lot of effort. Otherwise, use _choose file name_, and convert the result to a unix-style path using System Events, like we did before.


----------



## eliezer (Oct 23, 2005)

i dunno how to make an "on open file statement".

this is the code so far:


```
tell application "ffmpegX"
	if the title of window 1 is "Please register" then
		perform action button 1 of window 1
	end if
end tell
tell application "Finder"
	open document file "convert2mov.ffx" of folder "Documents" of folder "admin" of folder "Users" of startup disk using application file "ffmpegX.app" of folder "Applications" of startup disk
end tell
tell application "ffmpegX"
	tell the content view of window 1
		set the string value of text field 5 to (choose file)
		set the string value of text field 2 to "/Users/admin/Desktop/XYZ.mov"
		perform action button "Encode"
	end tell
end tell
```

for some reason it doesn't work. try it and you'll see the problems with it.
also, i'm not sure how to use (the_file) instead of (choose file). it says its undefined and it won't even let me drop movies on it anyway.

thanks


----------



## Mikuro (Oct 23, 2005)

Ah. An open statement looks like this:
	
	



```
on open {the_file}
	--do stuff here
end open
```
I've updated the script using the techniques I described in my last post, plus a couple more. It works on my end. Hopefully it'll work on yours, too. You'll need to update the path to the preset.

```
on open {the_file}
	tell application "ffmpegX"
		activate
		if the title of window 1 is "Please register" then
			perform action button 1 of window 1
		end if
	end tell
	tell application "Finder"
		--activate
		open file "Path:To:Preset.ffx" using application file ((the application file of application process "ffmpegX") as text)
		tell application "System Events"
			keystroke "
		"
			keystroke "
		"
			keystroke "
		"
		end tell
		open (the_file) using application file ((the application file of application process "ffmpegX") as text)
	end tell
	tell application "ffmpegX"
		activate
		tell application "System Events"
			keystroke "
		"
			keystroke "
		"
			keystroke "
		"
		end tell
		perform action (button "Encode" of the content view of window 1)
	end tell
end open
```
I'm sure you're wondering what the heck the deal is with all that keystroke nonsense. Well, I figured out why I kept getting "Error loading preset" dialog boxes. It was very strange. It seemed that even after opening the files, ffmpegX doesn't really _acknowledge_ them until you move the mouse or hit some keys. Again, very strange, but this is the kind of thing that happens when you try scripting non-scripting-savvy apps like this. So that keystroke thing is a quick & dirty workaround that seems to solve that problem by giving ffmpegX a quick kick in the pants, so to speak. Again, it works on my end. This kind of thing really IS something of a black art, so...if you're getting bizarre errors, you might need to play around with that bit of code.


----------



## eliezer (Oct 24, 2005)

thanks a lot mate


this is the path to the preset file, but i'm not sure how i put it into the script: /Users/admin/Documents/convert2mov.ffx

(open file "Path:Toreset.ffx" using application file ((the application file of application process "ffmpegX") as text) tell application "System Events")


----------



## Mikuro (Oct 24, 2005)

The difference here is between Carbon/Classic-style file paths and Unix/Cocoa-style file paths. AppleScript always takes Carbon-style (colon-delimited) paths. So...
/Users/admin/Documents/convert2mov.ffx
becomes
Macintosh HD:Users:adminocuments:convert2mov.ffx
(replace "Macintosh HD" with the name of your startup disk)

Alternatively, you could say something like this...
	
	



```
return item "convert2mov.ffx" of folder "Documents" of home
```
"home" is a property of the Finder that points to your Home folder, so you can use it in any _tell application "Finder"_ block.

And if you need to convert a Unix-style path to a Carbon-style path in the script itself, use the "posix file" command, like so:
	
	



```
return posix file "/Users/admin/Documents/convert2mov.ffx" --returns file "Macintosh HD:Users:admin:Documents:convert2mov.ffx"
```


----------

