mac-os-x

recent posts related to the mac os x operating system

AppleScript Unix Terminal: How to run an AppleScript from Unix

AppleScript Unix Terminal FAQ: How can I run an AppleScript script from the Mac Unix terminal (Unix command line)?

A cool thing about Mac OS X is that you can run AppleScript programs from the Unix shell. (Well, I guess it's cool if you're a Unix user, and I am.)

Running an AppleScript program from the Unix shell turns out to be surprising easy. For instance, if my current working directory has a script named OpenUrls.scpt in it, I can run that script from the command line like this:

AppleScript timer - run a script on a schedule with cron

Once you know that you can run an AppleScript program from the Unix command line it's easy to run it at scheduled intervals using the Unix cron facility. Here's an example of how to run my Safari script at the 7:30 a.m. every day.

30 7 * * * /Users/al/tmp/OpenUrls.scpt

I'm not going to get into the cron facility in any detail here, I just wanted to show how this is done. I will say that I modify the cron script by using this command:

AppleScript application - How to save a script as an application

AppleScript application FAQ: How do I save an AppleScript script as an AppleScript application?

I just finished writing an AppleScript program to open multiple URLs in multiple tabs in Safari, and -- being incredibly lazy -- I want to put this script on my desktop and have it run whenever I double-click it. That's all the work I'm willing to put into it, no other approach will do.

AppleScript Safari URL tip - how to open multiple URLs in Safari tabs

Well, I set out to write a simple tip on how to activate a Mac application using AppleScript, and I ended up writing a program to open a list of URLs in separate tabs in Safari using AppleScript. (Yeesh, I really took a detour. Oh, well.)

Without any further ado, here's the source code for this AppleScript program:

Create an AppleScript list with the every keyword

AppleScript list FAQ: How do I do something for every item in an AppleScript list? (or, How do I loop over an AppleScript list?)

Loop over an AppleScript list with the every keyword

I've shown examples of the AppleScript every keyword in other posts, so rather than create anything new, here's a repeat of one of those examples:

AppleScript tip: getting the current time

A quick note about getting the current time in AppleScript in two different formats. This command

set t to (time string of (current date))

yields something like this result (depending on the time of day):

"12:48:35 PM"

but this command

set t to (time of (current date))

yields something like this result:

46218

(That's the number of seconds since midnight, in case you were wondering.)

The AppleScript delay command

AppleScript delay command FAQ: Can you show an example of the AppleScript delay command?

If your AppleScript is running too fast you can slow it down with the delay command, like this example:

display dialog 1 buttons {"OK"}
delay 3
display dialog 2

When you click OK after the first dialog, your AppleScript program goes to sleep for three seconds, then displays the second dialog.

Hopefully you'll have more practical uses for this AppleScript sleep/delay command my sample code. :)

 

AppleScript dialog tip: customizing dialog boxes

AppleScript dialog FAQ: How can I customize AppleScript dialog boxes?

When you display an AppleScript dialog box you don't have to just use the standard Cancel and OK buttons, you can change the text to something more meaningful to your specific prompt. For instance, suppose you want to ask someone their age range:

display dialog "How old are you?" buttons {"Less than 1", "Older than 1"}

(Okay, not the greatest example in the world, but that's all I've got today.)

AppleScript repeat while examples

AppleScript while loop FAQ: Can you share some examples of the AppleScript while loop syntax (the AppleScript repeat while syntax)?

Instead of using a keyword like "while" to create loops, AppleScript uses the "repeat" keyword to create loops. There are several versions of this syntax, as follows.

AppleScript repeat syntax: Repeat X times

Here's how to repeat an AppleScript command X number of times:

AppleScript tip: if/then syntax

I've shown some basic AppleScript boolean-oriented syntax like this in another post:

set a to true
if a then
  display dialog a
end if

That code doesn't do too much, especially because you know the variable a is set to true, but it becomes a little more helpful when you don't know if a is true or false:

if a then
  -- do something really important here
end if

You can perform other if/then checks based on numerical tests, like this:

Syndicate content