101 Snippets


Ric

New Member
Joined
May 14, 2004
Messages
4,260
Reaction score
4
One of the nice things about any programing language is the ability to reuse bits from your previous work...

...so I shall put a few snippets in here that are common AppleScript routines...


Hope they help !

If you have any that you'd like to share please feel free !
 

Ric

New Member
Joined
May 14, 2004
Messages
4,260
Reaction score
4
Running a Shell Script as Root...from AppleScript

do shell script "periodic daily" password "password" with administrator privileges

--Open this script in a new Script Editor window.


bear in mind that this way your password would be stored within the AppleScript.


You can also do it this way, but the script will throw up a dialogue box asking for the password...

do shell script "periodic daily" with administrator privileges

--Open this script in a new Script Editor window.
 
Ad

Advertisements

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here's an easy one. It displays a dialog then opens a web site-
display dialog "Hey check this website out!"
property target_URL : "https://www.mac-help.com/"
open location target_URL
 

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here's another simple one but it's still one of my favorites-
on run
say "Hello. How are you today? Isn't it nice having a computer that will talk to you?" using "Vicki"
end run
 

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
U2FsdGVkX1/NLlJb285fLcFFmqC//raFSTimg0n2xYxXrhWzxhYe4Xs531onMQ7U
l1HXvOVzgIyB1OOT1szrZo/Dx8YAfuRdmj2MeAqbfHTYl7/w5JyGsL+rEqJ4qZ+M
FkmqRSEx3K0cqEv6ZgpCwglXQ5W7yWhjm7+xUb5oM6YCzExsE30Swk6tNKM+K34f
SscDpy0aZzdKqci0z1brwpUWXk6ZefaShwlQ5goCSzO0ZtLy/U+GII6Cto+iT0+c
2kFR5roYDPZo6w0ujAIAjNxmYjEP7jvHuYqS1OcvmtPXBdGzRFLuyS25bgxVLC8Y
1q4hFagYrksnLgCLvVTlv+DRIbEsgizVCwdZJa1u5jqBbVGQwoNXiDPvZ6Hj1UL5
p4ddM0ZmxaGap2/Xb/SKhrF/O6ia7jiZlexEQc4NldT6fdZmplKWyeanoOoCe9OG
88mPf6Te2I+n1amKPcQGbldA0tmbziUs3by7FmJPCB8GXH8bAHt6DKhEvgQ505iH
ZIAg1fs8Gj9p1MjfVXqkTbKbd4ZQJGiepf19h3o5+jMvHP3EMwYe0zOv6qncIv6/
BoGKifPFyWVgYHq3ylV2fYGru7rSDo2K9JgGeg3dgZsW0Ku3LKjtaseqi8fCIG7i
Effe2ldRrYnPo3uvE9X0V3AViJ4bWzNYQrNHeG0YMLv+WPAk4xT+eg==


http://snipr.com/1w33q-suqau3

Link is obsolete. ^^^^^

processInput("", "Encrypt")

(* * * * * * Handlers for encrypting the data * * * * * * * * * * *)
to eS(inS, inK, inM)
(* 'Encrypt String':
This handler uses the blowfish encryption available in openssl.
The handler is configured to accept the string to be encrypted, the key to use
(generated by the 'gK()' handler below), and the mode to pass to the openssl
call... either encrypt("e") or decrypt("d"). *)

return do shell script ("echo " & (quoted form of inS) & " | openssl enc -bf -" & inM & " -pass pass:" & (quoted form of inK) & " -salt -a")
end eS

to gK()
(* 'Get Key':
This handler assembles a password(encryption key) to use with the blowfish script
This is done to better hide the key in the script, so people reading through a
compiled, run-only script will have a difficult time determining what the key is. *)

set kL to {"jTiiGHa67567U2FsdGVkX184g", "C3w2235689mNVxw35467JnJMH", "JGdD34hn7n7N6bdFg67H6d54o"}
return ((characters 8 through 13 of (item 2 of kL)) & (characters 2 through 6 of (item 3 of kL)) & (characters 19 through 23 of (item 1 of kL))) as string
end gK

(* * * * * * Sample handler to show usage * * * * * * * * * * *)
to processInput(inString, inAction)
set outString to ""

if (inString is not "") then --> Process any incoming string
if (inAction is "Encrypt") then
set {encMode, inAction} to {"e", "Decrypt"}
else if (inAction is "Decrypt") then
set {encMode, inAction} to {"d", "Encrypt"}
end if
set outString to (eS(inString, gK(), encMode))
end if

set userInput to (display dialog "Your message:" default answer outString buttons {"Decrypt", "Cancel", "Encrypt"} default button inAction)
set {inString, inAction} to {(text returned of userInput), (button returned of userInput)}
processInput(inString, inAction)
end processInput
 

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
So, no one figured out the cryptic message in my previous post, huh? :D
Anyway, here's a script that's actually quite practical. It opens the trays to your drive(s). Ideal when the suckers don't want to open! I call it "DiscEjector". ;)
Ejector-


do shell script "drutil tray eject"
 

Attachments

Spawn_Dooley

Moderator
Joined
Jun 13, 2007
Messages
2,855
Reaction score
93
Hey, you're pretty handy with scripting Walrus! Did you teach yourself how to do it? So far I haven't gone beyond the compiled Mac OS X scripts .... I dragged your script into a Script Editor window and clicked on Run and was delighted to hear the results .... don't know what to do next ????





I just changed the text to "How are you today? Don't spend too much time on the forums."
 

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Hey, you're pretty handy with scripting Walrus! Did you teach yourself how to do it? So far I haven't gone beyond the compiled Mac OS X scripts .... I dragged your script into a Script Editor window and clicked on Run and was delighted to hear the results .... don't know what to do next ????





I just changed the text to "How are you today? Don't spend too much time on the forums."
Thanks Spawn. I have a few original scripts but many of the scripts that I use I get from the Net. I usually just modify them to my liking. Looking at your picture, I'm surprised that this script worked because of the letter "a" after end run. An identifier shouldn't be able to go after this command name. On this particular script you can change the voice that's used as well. Try changing "Vicky" to "Zarvox". :)
 

Spawn_Dooley

Moderator
Joined
Jun 13, 2007
Messages
2,855
Reaction score
93
Actually, the script wouldn't run because of that letter a ... I musta clipped the keyboard or something. I received an error msg and had to remove it, then it ran properly.
 

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Yep, one out of place character could render a script useless. Here is another script for you that performs a function (creating a folder on your desktop) then talks to you when it's complete. Of course, when you run across scripts that you like, you can save them. I usually save them as scripts. This way I can copy and paste the scripts in my apps and then edit them accordingly. You could also save your scripts and use them in Automator in conjunction with workflows and actions. :)
 

Attachments

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here is another simple one that shows you what you have copied on your clipboard. Of course, something has to be on your clipboard in order for this script to work.
 

Attachments

Spawn_Dooley

Moderator
Joined
Jun 13, 2007
Messages
2,855
Reaction score
93
That's really cool ! I put it into my OpenMenuX contextual menu section. ;)

 

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here's a pretty cool Script by TrashMan called DesktopSlideshow that prompts you to choose a folder of pictures and then presents them as a slideshow on your desktop.
 

Attachments

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here's a couple more easy ones. The first script will prepare an email with a subject and open your Mail app.
The second script will find an album by a particular artist on the iTunes Store. In this case it's set to find "Sticky Fingers" by the Rolling Stones. Of course you can change these according to your preferences.
 

Attachments

Walrus666

New Member
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here's one that will come in handy if you need to empty the trash that's filled with stubborn locked files. It's called "ForceEmptyTrash". :)
 

Attachments

Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top