EDITOR ’S NOTE : This clause was adjust fromAppleScript : The Missing Manual , by Adam Goldstein ( 2005 ; reprinted by permission of O’Reilly Media ) .
AppleScript has been around since the days of System 7 . Despite its matureness , snotty Mac programmers often look down on AppleScript for being too uncomplicated , too easy to learn , and too much like English . Of course , those are just the traits youwantin a computer language . Here are three cool thing AppleScript can do for you .
1. Slim Images Down in a Jiffy
Photoshop is the embodiment of pro image editing , but it ’s a memory and processorpig . If you ’d like a very simple graphics program — one that works completely in the background — OS X has your just the ticket . It ’s called Image Events .
For example , if you take pictures on a digital camera or use a scanner , then you ’ve likely got a bunch of bloated TIFF files that are far too large to e - mail or post on a entanglement page . So what should you do ? Convert your file using AppleScript .
If you look in Image consequence ’ dictionary , you ’ll obtain that the
bring through this script as an program by selecting File : Save As and choosing program from the File Format pop - up menu . Give it a memorable name ( such as Convert2JPEG.app ) . Now when you drag a lot of data file onto the hand ’s Finder image ( know as adropletbecause it runs an AppleScript when you drag and drop files or brochure onto it ) , you ’ll transform your once - fully grown images into summary JPEGs . Here ’s how the hand works :
Part 1 handles what happens if you simply twice - pawl on the droplet : the handwriting give a dialog box seat tell you to drag and deteriorate file instead .
Part 2 severalize AppleScript to iterate through the files you dropped one at a time , and to set the
Part 3 limit two other important variables :
Part 4 convert the epitome to a JPEG and saves it .
unluckily , Image Events is n’t smart enough to rename your file with a.jpgextension . That mean if your original file was called horses.tiff , your new JPEG file will still be call horses.tiff . This is a formula for massive confusedness when you attempt to get to the file in Adobe Photoshop or Apple Preview , since your image ’s extension service ( .tiff ) wo n’t match its existent format ( JPEG ) .
Part 5 , therefore , enjoin the Finder to rename your file with the right .jpg filename extension . The Finder does this by find the filing cabinet you require to rename (
2. Shave a Page Off Your Document
If you write alphabetic character and e - mail subject matter — or computer books , for that matter — you’ve probably have the direful one - page - too - recollective trouble . It lead like this : you ’re typing your text file , but when you make out to the terminal , you realise your written document is just one page longer than you desire it . And if you ’re like most people , you suspire profoundly and then get to work on shrink the individual face in your document . The trouble with this approach , of course , is that it can take a long time to select blocks of text and alter their font size , especially if you need to change multiple fonts on dozens of pages .
Microsoft Word provides an AppleScript command that takes care of all this school text - squeezing clientele for you :
The definition of the mastery tells you that it ’s intend to extort your fonts “ just enough so that the document will gibe on one fewer page . ” The light - blueing
If you dawn on OK , the hand proceeds with the next statement (
If you press Cancel , the script ends .
After you fall into place on OK , Word finds out what it ’ll take to knock one page off your text file and adjusts the font sizes throughout the text file .
3. Automatically Tell iChat That You’re Away
One of iChat ’s coolest features is that it lets you notify your friends as to whether you ’re around . When you ’re at your computer , you set your iChat status to Available ; when you ’re out , you set up your status to Away . handily for you , AppleScript can do the same matter mechanically .
Of of course , that ’s nothing really unique . To add material world power , you’re able to have your script set your condition to aside only if your silver screen saver is running .
Here ’s how :
Before your script will move properly , you have to choose File : Save . For File Format , choose diligence . Select the Stay Open option , so your book runs in the background all the time . keep the file somewhere accessible , because you could run it only by two-fold - clicking on its icon in the Finder .
Here ’s how the computer code work :
Part 1 is anidle handler . To AppleScript , that means “ lead the following computer code whenever this book is n’t meddlesome doing something else . ” To you , it mean the script will run forever .
Part 2 tell System outcome to get a list of all presently running programs on your arrangement . Then the book checks to see whether that list contains Screen - SaverEngine — OS X ’s covert - saver program . In other Son , part 2 check whether your screen recoverer is running . If it is , the script proceeds to part 3 .
Part 3 adjust your iChat status to Away if your projection screen saver is run for . That way , your admirer wo n’t test to chat with you .
Part 4 say the idle coach to check over back again in 10 second . The result is that your playscript stop every 10 seconds to see whether your CRT screen saver is run .
When your screen saver is n’t running , the only meter reading you ’ll have that your handwriting is running is that its ikon will appear in the Dock . Because you turn on Stay capable for the script , the code continues checking your screenland recoverer evermore ( or at least until you ensure - tap the script ’s icon in the Dock and pick out Quit ) .
[ Adam Goldstein is the teenage founder ofGoldfishSoft , a software company specializing in secret plan and service program for OS X. Through June 1 , 2005 , you may get a 25 percent rebate and free merchant vessels when you buy his bookAppleScript : The Missing Manual . At checkout , apply discount code D5APSC . ]