101 Snippets

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
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

Joined
May 14, 2004
Messages
4,260
Reaction score
5
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.
 
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
 
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
 
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

  • DiscEjector.scpt.zip
    835 bytes · Views: 400

Spawn_Dooley

Moderator
Joined
Jun 13, 2007
Messages
2,870
Reaction score
94
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 ????

20080326-rwyuwa348hi1bcb434e1jcjfte.jpg




I just changed the text to "How are you today? Don't spend too much time on the forums."
 
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 ????

20080326-rwyuwa348hi1bcb434e1jcjfte.jpg




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,870
Reaction score
94
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.
 
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

  • SpawnsScript.scpt.zip
    1.5 KB · Views: 450
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

  • Clipboard Viewer.scpt.zip
    574 bytes · Views: 422
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here's five more for you. Be careful with these! I was going to go into detail about these but their names pretty much tell what they do. ;)
 

Attachments

  • DeleteFilesInAFolder.scpt.zip
    1.2 KB · Views: 429
  • DeleteFoldersInAFolder.scpt.zip
    1.2 KB · Views: 424
  • DeleteEverythingInAFolder.scpt.zip
    1.2 KB · Views: 417
  • DeleteFolder.scpt.zip
    1.3 KB · Views: 427
  • DeleteFile.scpt.zip
    1.4 KB · Views: 451
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

  • DesktopSlideshow.scpt.zip
    2.3 KB · Views: 400
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here is another script that names folders, called; you guessed it, FolderNamer! :D
 

Attachments

  • FolderNamer.scpt.zip
    1.7 KB · Views: 424
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

  • MailToWithSubject.scpt.zip
    1 KB · Views: 407
  • iTunesAlbumFinder.scpt.zip
    1.1 KB · Views: 410
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

  • ForceEmptyTrash.zip
    1.3 KB · Views: 647
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here's one that I came up with that will create a folder on your desktop with a selected name and it's called, you guessed it, "CreatingAFolderWithASelectedName". :D
 

Attachments

  • CreatingAFolderWithASelectedName.scpt.zip
    1.5 KB · Views: 391
Joined
Jul 21, 2007
Messages
1,252
Reaction score
15
Here's another very simple script that closes the front window and it's called...... ah, you know what it's called. :D
 

Attachments

  • CloseTheFrontWindow.scpt.zip
    1.2 KB · Views: 402

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

Similar Threads

Dock icon OSX 1013.2 5

Top