Being respectful to traditions, let us start with a script for drawing “Hello World” in Photoshop. One can use Javascript or VBScript in windows or Applescript in Mac for photoshop scripting. The following Javascript code will produce a “Hello World” photoshop document.
var originalUnit = preferences.rulerUnits preferences.rulerUnits = Units.PIXELS // Create a new document var docRef = app.documents.add( 300, 300 ) // Create a new layer var artLayerRef = docRef.artLayers.add() artLayerRef.kind = LayerKind.TEXT // Write our text artLayerRef.textItem.contents = "Hello World" // Release references docRef = null artLayerRef = null textItemRef = null // Restore unit setting app.preferences.rulerUnits = originalUnit
Code explained :
First of all, we need to keep record of the current unit settings since we are going to change the unit settings according to our preferences. After saving the current units settings, we change the units to PIXELS.
Next, we create a 300X300 pixel document. Then we add a new layer to the new document. Since we want this to be a text layer, we set LayerKind to TEXT.
On the new layer, we write our text, “Hello World”.
At the end, we dereference the variables and restore the previous unit settings.
Javascript function to get current window height and width. Works with most of the browsers.
// function to get the current width of the window
function getWidth(){
var x = 0;
if (self.innerHeight){
x = self.innerWidth;
}else if (document.documentElement && document.documentElement.clientHeight){
x = document.documentElement.clientWidth;
}else if (document.body){
x = document.body.clientWidth;
}
return x;
}
// function to get the current height of the window
function getHeight(){
var y = 0;
if (self.innerHeight){
y = self.innerHeight;
}else if (document.documentElement && document.documentElement.clientHeight){
y = document.documentElement.clientHeight;
}else if (document.body){
y = document.body.clientHeight;
}
return y;
}
N.B. Originally posted @ my personal blog
The usual Timer class is available in Android, but unless you are doing non-trivial stuff, its better to use Handler for scheduling task. Why? Well, Timer class introduces a new thread and while developing mobile application, you should really think twice (or more than that!) before using it.
So, how jobs can be scheduled using Handler class? There are two different approaches. First, You can ask it to send messages at some later time by using sendMessageAtTime(Message, long) or sendMessageDelayed(Message, long) , which then can be processed as shown in my previous post, effectively acting as a timer. Secondly, you can directly ask it to run some scheduled task by using postAtTime(Runnable, long) and postDelayed(Runnable, long).
The methods described here lets you schedule a single shot task. To repeat a scheduled task, you have to register it again to run for the next time. So, assuming that you want to repeat a task after one second delay, we can do something like this,
private Handler handler = new Handler(); private Runnable runnable = new Runnable() { public void run() { doStuff(); /* * Now register it for running next time */ handler.postDelayed(this, 1000); } };
To stop a repeated task you can use removeCallbacks(Runnable r), which removes all pending Runnable r in the message queue.
Note: This article was originally posted on my personal blog.
Facebook is announcing developer fund once again! Submission deadline is April 20. Any idea?
Search
News
Blog
- Pass variables into flash
- Fetching data from multiple tables in one MySQL query – Part 2
- Fetching data from multiple tables in one MySQL query
- Let’s Go A Hunting
- Java Applet and Javascript message passing
- The Right-Click Menu in Flash
- Stroke hinting
- Python: C++ style cin, cout in Python
- Python: Working in Unicode
- Website Security : Directory Listing Issue


March 20, 2009 in
