08.13.07
AppleScript
As you can see if you take a look at my tag-cloud, I really like AppleScript. It is so easy to implement simple tasks with a few lines. Most in OS X included applications support it and also many others. Shell scripts are also includable. It is so easy if you compare to other systems. Most parts of Linux are of course highly scriptable which I really like. It is a bit catchy to get into that but surely remunerative. Anyway, with AppleScript it is really easy to take the first steps. You can tell application “Finder” to empty the trash. Just as I wrote it.
tell application "Finder" to empty the tash
Or lets say "Hello World".
You can control many apps and your system. But not only that. It can handle variables, loops, functions and whatnots.
Take a look if you want to get more out of your Mac. There is a great page with many resources: http://macscripter.net/
I also recommend this free book for AppleScript starters by Bert Altenburg. http://www.fischer-bayern.de/as/as4as/AS4AS_e.pdf It teaches the basics. And with the help of the functionlist (+shift+o in the scripteditor) you have the knowledge to create quite useful, individual tools.
08.10.07
iCal countdown
I wrote an other little AppleScript for someone. It creates events in iCal as a countdown. First, you can choose one of your calendars. Then enter the start- and enddate. It will count down and say every day “x days left”. I tested it with my local date format, but it is supposed to work anywhere since the Mac knows his date format.
on run
tell application “iCal”
set cal_list to {}
repeat with x from number of calendars to 1 by -1
set the beginning of cal_list to name of calendar x
end repeat
set cal_list_string to “”
repeat with x from 1 to number of calendars
set cal_list_string to cal_list_string & (x as text) & ” – ” & item x of cal_list & return
end repeat
end tell
display dialog cal_list_string default answer “”
set cal_number to text returned of result as number
display dialog “start date” default answer “”
set anfangsdatum to date (text returned of result)
display dialog “end date” default answer “”
set enddatum to date (text returned of result)
repeat with anzahl_tage from 1 to 99999
if anfangsdatum + (anzahl_tage * 3600 * 24) is enddatum then
exit repeat
end if
end repeat
tell application “iCal”
tell calendar cal_number
set x_date to anfangsdatum
repeat with x from anzahl_tage to 0 by -1
set x_date to x_date + (3600 * 24)
set event_summary to “x & ” days left”
if x is not 1 then
set event_summary to “x & ” days left”
else
set event_summary to “one day left”
end if
make new event at the beginning with properties {summary:event_summary, start date:x_date, end date:x_date}
end repeat
end tell
end tell
end run
08.01.07
OmniOutliner to folder structure
An other useless AppleScript. OmniOutliner seems to be bundled with all new Apple computers. My idea is: Create a hierarchical structure in OO related to some kind of a project and export this to a folder structure in the filesystem to fill it with any kind of information.
The script exports a OPML file from OO to home/scriptfiles/ and reads the XML information out of it. It asks you for a folder to export the sub- and sub-sub… folders there. The OPML file stays, the script could be told to delete it. I like to stay.
I want to thank Skeeve who helped me with the handler which creates the folders. Just in case he reads this…..
on run
set inbetween to POSIX path of (path to home folder) & "scriptfiles/"
tell application "Finder"
try
make new folder at ((home as text)) with properties {name:"scriptfiles"}
end try
end tell
tell application "OmniOutliner"
export document 1 to inbetween & "oo.opml" as "OOOPMLDocumentType"
set selected_path to (choose folder) as string
set destination to POSIX path of selected_path
end tell
tell application "System Events"
set XMLfile to XML file (inbetween & "oo.opml")
set root to XML element 1 of XMLfile
set body to XML element "body" of root
my make_structure(body, destination)
end tell
end run
to make_structure(parent, current_path)
set done to false
tell application "System Events"
repeat with elt in (every XML element of parent whose name is "outline")
my make_structure(elt, current_path & (value of XML attribute "text" of elt) & "/")
set done to true
end repeat
end tell
if not done then
do shell script "mkdir -p " & quoted form of current_path
end if
end make_structure
Update on the Bookmarking thing
There was a bug in this script. It took the wrong URL when multiple pages were opened. Never trust code from some stranger, I tell you. There is still a flaw, sometimes it changes the active Safari window for some or no reason. Buuut: Now there is a list of the acronyms and what’s their meaning. And the input works a bit different: type the acronym followed by a space and then the description. Page creation or direct addressing by pagename does not work anymore.
Here is the code. I edited it also in the previous post.
Edit: Ok, I hope I killed the flaw now. Script is updated again.
on run
tell application "Safari" to set new_bm_url to URL of document 1 of window 1
activate
set acronyms to {"s", "a", "g", "o", "d", "l"}
set pagenames to {"bm Science", "bm Apple", "bm Games", "bm Other", "bm Software Dev", "bm Read Later"}
set d_l to ""
repeat with x from 1 to number of items of acronyms
set d_l to d_l & item x of acronyms & " - " & item x of pagenames & return
end repeat
display dialog d_l as text default answer ""
set text_result to text returned of result
set acronym to first character of text_result
set bookmark_note to characters 3 thru (number of characters of text_result) of text_result as text
tell application "VoodooPad"
repeat with x from 1 to number of items of acronyms
if item x of acronyms is acronym then
set itemnumber to x
end if
end repeat
set pagename to item itemnumber of pagenames
append text bookmark_note & return & new_bm_url & return to page pagename of document 1
end tell
end run
07.30.07
Bookmarking with VoodooPad
Bookmarks in the browser (doesn’t matter which one) does not work for me. I figured this out yesterday after years of webbrowsing. Somehow I can never remember what categories I had or why I bookmarked this particular page. With some browsers you can add a comment to a bookmark, but you have to open the bookmark manager every time. There may be some plugins, but I want to stay flexible in the choice of my browser. Yes, it’s kind of weird. Anyway, I deliberated if I should just copy the links to my favourite app, VoodooPad. That’s soo last century. But wait, there’s this cool method of automation, AppleScript. So I wrote a little script which asks me for a page to append the bookmark. I defined some acronyms, “s” for science for example. And then it asks me for a note, so I can type in a few related words to remenber the content of the website. Still last century? Maybe. I don’t care, it’s much more pleasing to me this way. You could fill in your acronyms and pagenames in the lists at the beginning of the script. It behaves like this: If you use the acronym, it appends the Bookmark to its page. If you type in something else, it appends the bookmark to the page with this name and creates a new one if this page does not exist. Here is it, in case someone is interested. Which is quite unlikely.
on run
tell application "Safari" to set new_bm_url to URL of document 1 of window 1
activate
set acronyms to {"s", "a", "g", "o", "d", "l"}
set pagenames to {"bm Science", "bm Apple", "bm Games", "bm Other", "bm Software Dev", "bm Read Later"}
set d_l to ""
repeat with x from 1 to number of items of acronyms
set d_l to d_l & item x of acronyms & " - " & item x of pagenames & return
end repeat
display dialog d_l as text default answer ""
set text_result to text returned of result
set acronym to first character of text_result
set bookmark_note to characters 3 thru (number of characters of text_result) of text_result as text
tell application "VoodooPad"
repeat with x from 1 to number of items of acronyms
if item x of acronyms is acronym then
set itemnumber to x
end if
end repeat
set pagename to item itemnumber of pagenames
append text bookmark_note & return & new_bm_url & return to page pagename of document 1
end tell
end run
07.26.07
A crappy map for VoodooPad
There’s this Application called VoodooPad. It’s a personal Wiki where I write down just everything.
I am a visual learner and would love to have a map view of the pages. I guess there are too many feature requests and not enought time. So I looked around for an external solution and found Graphviz. This is an open source tool for representing structural information. I tried to write a little AppleScript which exports the information into a .dot file which will be opened by Graphviz.
It works, but not very well. It just takes a list of all pages and searches in the contents for these strings for pagenames. Then writes it to the .dot file which is located at home:scriptfiles:dot: and tells the Finder to open the file using Graphviz which must be locted in the applications folder.
Some bugs are:
- it shows just small letters
- if there is a for example a page called “funny”, the string “fun” refers to it
- no links to files are recognized
- to be continued…
Other problems are:
- it’s quite mazily if there are too many pages
- it takes some time to process if the document is more than just a few pages
- it’s not interactive, you can’t move the page representations nor navigate
- you would have to run the script every time you change something
- not linked pages are not displayed
- to be continued… this list is probably endless
There are some settings in Graphviz which could make it a bit more acceptable. I use the “Energy Minimized” Layout and tell it to not overlap. These settings can be saved as default.
Even with these deficiencies I use it and hope that the idea has some potential. I really need the map view… it helps me to get an idea of that information I have.
Here is the script for the case, someone is interested. Please forgive me for this crap. I’m obviously not a developer.
Code:
on run
set ignorelist to {"index", "post im forum", "vp to graph", "vp to graph mit ignorelist"}
set AppleScript's text item delimiters to ""
set homepath to path to home folder as text
set dotfile to homepath & "scriptfiles:voodoodot2.dot"
tell application "Finder"
try
make new folder at ((home as text) & "scriptfiles:") with properties {name:"dot"}
end try
end tell
tell application "VoodooPad"
set number_of_pages to the number of pages of document 1
set pagelist to {}
set dotfile_refn to open for access file ¬
dotfile with write permission
write "digraph untitled {" & return to dotfile_refn
repeat with i from number_of_pages to 1 by -1
set the beginning of pagelist to name of page i of document 1 as text
end repeat
repeat with x from number_of_pages to 1 by -1
set pagecontent to (text of page (item x of pagelist as text) of document 1) as string
repeat with y from number_of_pages to 1 by -1
if pagecontent contains item y of pagelist and x is not y and ignorelist does not contain item x of pagelist and ignorelist does not contain item y of pagelist then
write "\"" & item x of pagelist & "\"->\"" & item y of pagelist & "\";" & return to dotfile_refn
end if
end repeat
end repeat
write "}" & return to dotfile_refn
close access dotfile_refn
tell application "Finder"
open document file dotfile using application file "Graphviz.app" of folder "Applications" of startup disk
end tell
set AppleScript's text item delimiters to return
end tell
end run
Here is a picture.
It’s just a simple example. Most likely it does’t look so nice in a real world scenario. Oh, there’s also an ignore-list. Pages in this list will not be displayed.
Thanks a lot!
Lazra