Assign hotkeys to scripts in MAC OS X - I found a way

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Efraim
Posts: 2
Joined: June 5th, 2008, 9:37 am
Location: The Netherlands

Hi all, new here.

After a day of searching the web, trying out things and writing my first AE script for my hugely repetitive task, I found a way to assign hotkeys to scripts, using Applescript and Quicksilver. Through Applescript, you can run your AE (jsx) script from outside of AE, as documented in the scripting manual.
In my case, calling the script file didn't work, but pasting the entire script code in my Applescript did (be sure to escape your double quotes!).
So, if I ran the Applescript, it actually executed the things I wanted inside AE.

Now, with Quicksilver, you can add custom triggers, basically hotkeys to files, apps or other actions. If you create a custom trigger with your applescript as destination, it will run this. In my case, hitting Ctrl-§ would trigger my script (choose an unused key combination, ofcourse).
This all works perfectly, and it might save you some time and strain from all that repetitive mousing action :)

Quicksilver can be found here

My jsx script (which toggles the opacity between 0 and 100, creating a keyframe 1 frame before the current time holding the current opacity, and creating a keyframe at the current time with the new opacity):

Code: Select all

{
	// OpacityToggle.jsx
	// 
	// Author: Emar Vegt, 2008

	function OpacityToggle(myComp){
		var comp_layers = myComp.layers;
		var length 	= myComp.selectedLayers.length;

		var SNA 	= new Array();
		var curOp;		
		var curOp;
		var newOp;
		var curTime = myComp.time;
		var prevFrameTime = curTime - ( 1 / myComp.frameRate ); // time of previous frame
		
		if (length > 0) 			// only process 2 or more selected layers
		{
			for(j = 0; j < length; j++)	// collect in array SNA all layers which should we reordered
			{
				SNA[j]=myComp.selectedLayers[j].index;
			
//**
			
				cAi=SNA[j]; 			// Index of current layer A, top
				var lTMA = comp_layers[cAi];	// assign lTMA to current layer A
				Op = lTMA.property("Transform").property("Opacity");
				curOp = Op.valueAtTime(curTime,false );
				
				if (curOp == 100) { newOp = 0; } 
				else { newOp = 100; }
				
				Op.setValueAtTime(prevFrameTime, curOp);
				Op.setValueAtTime(curTime, newOp);				
//**
			} // end for-loop
		}
	}

	var proj = app.project;
	var undoStr = "OpacityToggle";

	if (proj){
		var activeItem = app.project.activeItem;
		if (activeItem != null && (activeItem instanceof CompItem)){
			app.beginUndoGroup(undoStr);
			OpacityToggle(activeItem);
			app.endUndoGroup();
		} else {
			alert("Please select an active comp to use this script");
		}
	}
	else
	{
		alert("Please open a project first to use this script.");
	}
}
Which is turned into the following Applescript:

Code: Select all

tell application "Adobe After Effects CS3"
	DoScript "
	function OpacityToggle(myComp){
		var comp_layers = myComp.layers;
		var length 	= myComp.selectedLayers.length;

		var SNA 	= new Array();
		var curOp;		
		var curOp;
		var newOp;
		var curTime = myComp.time;
		var prevFrameTime = curTime - ( 1 / myComp.frameRate ); // time of previous frame
		
		if (length > 0) 			// only process 2 or more selected layers
		{
			for(j = 0; j < length; j++)	// collect in array SNA all layers which should we reordered
			{
				SNA[j]=myComp.selectedLayers[j].index;
			
//**
			
				cAi=SNA[j]; 			// Index of current layer A, top
				var lTMA = comp_layers[cAi];	// assign lTMA to current layer A
				Op = lTMA.property(\"Transform\").property(\"Opacity\");
				curOp = Op.valueAtTime(curTime,false );
				
				if (curOp == 100) { newOp = 0; } 
				else { newOp = 100; }
				
				Op.setValueAtTime(prevFrameTime, curOp);
				Op.setValueAtTime(curTime, newOp);				
//**
			} // end for-loop
		}
	}

	var proj = app.project;
	var undoStr = \"OpacityToggle\";

	if (proj){
		var activeItem = app.project.activeItem;
		if (activeItem != null && (activeItem instanceof CompItem)){
			app.beginUndoGroup(undoStr);
			OpacityToggle(activeItem);
			app.endUndoGroup();
		} else {
			alert(\"Please select an active comp to use this script\");
		}
	}
	else
	{
		alert(\"Please open a project first to use this script.\");
	}
	"
end tell
Let's put it to good use! :)
Make it.
ajk48n
Posts: 20
Joined: January 17th, 2007, 3:02 pm

Correct me if I'm wrong, but I believe this can also be done, for the first 20 scripts or so, by setting the hotkey in the Shortcut preference file.
This can be found in Computer / Users / User Name / Library / Preferences / Adobe / After Effects / 8.0 / Adobe After Effects 8.0 Shortcuts

Search in this file for the line ExecuteScriptMenuItem02, and within the parentheses that follow, you can add a shortcut. There are only 20 items here though, so it may not work for all your scripts, which in that case, your idea may work better.

Also, ExecuteScriptMenuItem01 is, I believe, the command "Open Script Editor", not the first script in your list.
User avatar
Atomic
Posts: 157
Joined: April 30th, 2007, 5:55 am
Location: United States, Ohio

Whats wrong with using IN/OUT points for a layer? (Isn't that essentially what you are diong?)

I don't understand the point of your script?
"Up And Atom

No...No

Up And At Them!"
Efraim
Posts: 2
Joined: June 5th, 2008, 9:37 am
Location: The Netherlands

ajk48n wrote:Correct me if I'm wrong, but I believe this can also be done, for the first 20 scripts or so, by setting the hotkey in the Shortcut preference file.
This can be found in Computer / Users / User Name / Library / Preferences / Adobe / After Effects / 8.0 / Adobe After Effects 8.0 Shortcuts

Search in this file for the line ExecuteScriptMenuItem02, and within the parentheses that follow, you can add a shortcut. There are only 20 items here though, so it may not work for all your scripts, which in that case, your idea may work better.

Also, ExecuteScriptMenuItem01 is, I believe, the command "Open Script Editor", not the first script in your list.
Ah, I didn't know about that one. As I have only one script at the moment, that would have worked equally well and saved me the time of figuring out applescript. :roll:
(the reason that I tried this method in the first place was that someone said on this forum that you can't assign hotkeys to scripts within AE..)

And the point of my script:
I have 120 rectangles, each on its own layer, on its own position in the comp. They need to be visible according to a specific pattern, meaning that they pop in and out lots of times. I have to check which one should appear on a frame by frame basis, and modify the timeline accordingly. That's a lot of keyframes. It's already mind numbing using the script to set these, let alone if I would do it by hand.
Make it.
Post Reply