101 Snippets

Discussion in 'AppleScript' started by Ric, Nov 11, 2005.

  1. Ric

    Ric New Member

    Joined:
    May 14, 2004
    Messages:
    4,260
    Likes Received:
    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, Nov 11, 2005
    #1
    1. Advertisements

  2. Ric

    Ric New Member

    Joined:
    May 14, 2004
    Messages:
    4,260
    Likes Received:
    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.
     
    Ric, Nov 11, 2005
    #2
    1. Advertisements

  3. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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, Dec 1, 2007
    #3
  4. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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, Dec 2, 2007
    #4
  5. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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, Dec 30, 2007
    #5
  6. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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"
     

    Attached Files:

    Walrus666, Jan 2, 2008
    #6
  7. Ric

    Spawn_Dooley Moderator

    Joined:
    Jun 13, 2007
    Messages:
    2,632
    Likes Received:
    69
    Location:
    van diemen's land
    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 ????

    [​IMG]



    I just changed the text to "How are you today? Don't spend too much time on the forums."
     
    Spawn_Dooley, Mar 26, 2008
    #7
  8. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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". :)
     
    Walrus666, Mar 26, 2008
    #8
  9. Ric

    Spawn_Dooley Moderator

    Joined:
    Jun 13, 2007
    Messages:
    2,632
    Likes Received:
    69
    Location:
    van diemen's land
    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.
     
    Spawn_Dooley, Mar 26, 2008
    #9
  10. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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. :)
     

    Attached Files:

    Walrus666, Mar 27, 2008
    #10
  11. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    Here is another script that shows your IP Address.
     

    Attached Files:

    Walrus666, Mar 27, 2008
    #11
  12. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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.
     

    Attached Files:

    Walrus666, Mar 27, 2008
    #12
  13. Ric

    Spawn_Dooley Moderator

    Joined:
    Jun 13, 2007
    Messages:
    2,632
    Likes Received:
    69
    Location:
    van diemen's land
    That's really cool ! I put it into my OpenMenuX contextual menu section. ;)

    [​IMG]
     
    Spawn_Dooley, Mar 28, 2008
    #13
  14. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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. ;)
     

    Attached Files:

    Walrus666, Mar 31, 2008
    #14
  15. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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.
     

    Attached Files:

    Walrus666, Apr 20, 2008
    #15
  16. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    Here is another script that names folders, called; you guessed it, FolderNamer! :D
     

    Attached Files:

    Walrus666, Apr 21, 2008
    #16
  17. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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.
     

    Attached Files:

    Walrus666, Apr 22, 2008
    #17
  18. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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". :)
     

    Attached Files:

    Walrus666, May 10, 2008
    #18
  19. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    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
     

    Attached Files:

    Walrus666, May 27, 2008
    #19
  20. Ric

    Walrus666 Moderator

    Joined:
    Jul 21, 2007
    Messages:
    1,252
    Likes Received:
    12
    Location:
    Chicago
    Here's another very simple script that closes the front window and it's called...... ah, you know what it's called. :D
     

    Attached Files:

    Walrus666, Jun 8, 2008
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Newsbot

    Mac 101: Exposé

    Newsbot, Dec 21, 2006, in forum: Apple News
    Replies:
    0
    Views:
    567
    Newsbot
    Dec 21, 2006
  2. Newsbot

    Mac 101: Connect to the Internet

    Newsbot, Dec 21, 2006, in forum: Apple News
    Replies:
    0
    Views:
    601
    Newsbot
    Dec 21, 2006
  3. Newsbot

    Mac 101: Connect Your Printer

    Newsbot, Dec 21, 2006, in forum: Apple News
    Replies:
    0
    Views:
    644
    Newsbot
    Dec 21, 2006
  4. Newsbot

    Mac 101: Change Your Icons

    Newsbot, Dec 21, 2006, in forum: Apple News
    Replies:
    0
    Views:
    542
    Newsbot
    Dec 21, 2006
  5. Wikicodia Admin

    Wikicodia - The code snippets wiki

    Wikicodia Admin, Jul 27, 2007, in forum: Apps
    Replies:
    1
    Views:
    274
    Heath Raftery
    Aug 2, 2007
  6. Wikicodia Admin

    Wikicodia - The code snippets wiki

    Wikicodia Admin, Jul 27, 2007, in forum: Mac
    Replies:
    2
    Views:
    308
    Kurt Ullman
    Jul 27, 2007
  7. Jo_y

    Snippets Contents

    Jo_y, Nov 20, 2008, in forum: AppleScript
    Replies:
    0
    Views:
    297
  8. ph03nix2s
    Replies:
    3
    Views:
    349
    Spawn_Dooley
    May 14, 2013
Loading...