Posts filed under 'coding'

Scraping Source in Safari

Here is an applescript solution for grabbing the html code from a page online. This is particularly handy if you are trying to grab the code from a page that you need to login to. I am sure there is a much better solution out there, but this one seems to work for me ok.

CODE:
  1. -- Define the page to save the document and the url
  2. set the pageFile to "/Users/yourUserNameHere/Desktop/safariSource.html"
  3. set the pageUrl to "http://www.plasticstare.com/"
  4.  
  5. -- define the applescript to run
  6.  
  7. tell application "Safari"
  8.    activate
  9.    make new document at end of documents
  10.    set URL of document 1 to pageUrl
  11. end tell
  12.  
  13. set web_page_is_loaded to false
  14. --check if page has loaded
  15. repeat
  16.    delay 0.5
  17.    tell application "System Events" to tell application process "Safari"
  18.       if (name of static text 1 of group 1 of window 1 as text) begins with "Contacting" or (name of static text 1 of group 1 of window 1 as text) begins with "Loading" then
  19.          -- do nothing
  20.       else
  21.          exit repeat
  22.       end if
  23.    end tell
  24. end repeat
  25.  
  26. tell application "Safari"
  27.    set siteSource to the source of document 1 as text
  28.    set theFile to open for access (pageFile) as POSIX file with write permission
  29.    set eof of theFile to 0
  30.    write siteSource to theFile
  31.    close access theFile
  32. end tell

Add comment Sun, 2008 Mar 30, 6:19pm

Delicious for Wordpress

Props to Rick for this simple way for adding recent delicious bookmark postings to your wordpress sidebar. Download Delicious for Wordpress here: http://rick.jinlabs.com/code/delicious/

It was the simplest way I could find to do this. It isn't customizable, other than editing the code yourself, but it does a great job out of the box, and does pretty much what you would want it to do anyway, which is post things to your blog that you found interesting, without having to actually write a post about it...hooray for industrious laziness!

Add comment Sun, 2008 Mar 30, 4:50pm

Copy and continue copying large files over file sharing

In my experience, there is often a flaky slow server at work that contains vital files on it. This is fine when you need to retrieve files that are small, but what happens when you need to copy a large file? Most of the time these servers don't support ftp protocol and if you are on a Mac, you have to connect via smb (Samba) to the share and start copying and hope you don't have to stop, because if you stop the copy, the file disappears from your machine and you can't resume it.

However, I stumbled on this gem the other day:

curl -C - -O file:///Volumes/samba_share/file/path/here.txt

Basically, curl allows you to copy a file with url syntax in the terminal. The capital "O" tells curl to copy the file to your desktop with the same name as the file on your server, and the capital "C" tells it to continue where it may have left off. You can monitor the download in the terminal window. If you need to leave work and finish the copy the next day, simply enter the command again in the terminal and it will pick up the copy where it left off.

Add comment Thu, 2008 Mar 20, 11:05am

Applescript Show Track in Finder

Space has been tight lately on my laptop and I've been too lazy to thoroughly go through and clean up. I keep regular backups, however, so I wrote this script so that when I hear a track that I don't care so much about in iTunes on random, I can go to the folder and kill the track or the album, or heck, all tracks by that artist, quickly. You can put this file in your iTunes scripts folder and trigger it from Quicksilver as well for easy access.

PlasticWare Downloadable ::
Show Track in Finder (Mar. 11, 2008)
Download (~3,146 bytes) :: ShowTrackInFinder.zip

Add comment Tue, 2008 Mar 11, 1:44pm

Chumby Widget 002 - Japanese Alphabet!

20080126120743_w810i.jpgJust finished up version 1.0 of the Japanese Syllabary Flash Card for the Chumby. It was approved in its version 0.1 stage last week, but unfortunately, when you update a widget, it has to be approved again for public viewing. Perhaps after the big official Chumby launch there will be different tier developer permissions, some of which would allow for updating already approved widgets without further approval. I guess we shall see about that. So, I expect this one to go public again on Monday. I'll post the link for the Chumby site page for it once it is up again. In the meantime, you can view a non-configurable version on my site at http://www.plasticstare.com/nihongo/syllabaryflashcard.html. The photo is of a slightly earlier version than the current one, but it looks pretty much the same. The Chumby widget allows you to view Hiragana or Katakana (or random), allows you to choose which of the Romaji and symbol to view first, and allows you choose which character sets you wish to view (and defaults to a,i,u,e,o,ka,ki,ku,ke,ko, and ga,gi,gu,ge,go).

Update: the chumby widget can be found at http://www.chumby.com/guide/widget/Japanese Syllabary Flash Card. Enjoy!

1 comment Sun, 2008 Jan 27, 4:09am

Bizarre Clipboard Problem

This has happened to me a couple of times using TextMate (probably has occurred
elsewhere without consequences that I noticed). Every once in a long while, I will not be able to copy text into TextMate. I do this on a regular basis to analyze XML and JSON responses from the server for the application I work on at VmWare, copying FireBug response data into TextMate running either the XML tidy or a script I set up to tidy up the JSON data, so copying into TextMate is very important to me. Sometimes, however, it just doesn't work. I can copy/paste in TextMate, but not from another application to it. As it turns out, this has to do with the system pasteboard server. Essentially, you can fix the problem by restarting this service. See: http://wiki.macromates.com/Troubleshooting/CantPaste for more details.

Add comment Wed, 2008 Jan 09, 11:47am

Data Anomolies

I've been poking around lately working on getting some design ideas together and investigating some technical possibilities, as well as backing up some old data. Erasing some of that old data resulted in the crazy progress bar pictured above. I've also discovered a really frustrating problem while investigating pulling Flickr streams.

It seems that you can do a few nice little searches that aren't well publicized, so that you can, for instance, get an RSS stream featuring the union of two different Flickr tags in your stream (in other words, an OR search). The feed URL looks something like this (for the tags blue and green):

feed://api.flickr.com/services/feeds/photos_public.gne?id=74517463@N00&tags=blue,green&tagmode=any&lang=en-us&format=rss_200

Replace the "tagmode" parameter with all, and you get an AND style search (only photos featuring all the specified tags). The problem is that the RSS feed provided by Flickr only allows you to grab 20 photos. If you perform the same search, you get a large amount of paged data, if there is more than one page. So, basically, this means that if you want to see ALL the photos of your children in your own home-grown chumby, OSX, or other widget, you are sadly stuck at seeing only 20.

"What about using something like Pipes?", you say. Well, I checked out using Yahoo pipes to create a custom RSS feed. Unfortunately, from what I can tell, you are really limited to what you can get via the Flickr API. You can get more than 20 photos in 1 person's photostream this way using the flickr.people.getPublicPhotos method, but that method does not provide the capability of filtering by tag.

I'd just about given up (well I still have for now) until I realized that you can do just this in the media bar in Flock, so there must be a way to do it that I am missing. In Flock, you can view your photostream and *fliter* by tags, and even use AND and OR for multiple tags. Not sure how they are doing this, because you can see way more than 20. I sent them an email to see what they are doing today. Anyone else have any idea?

Add comment Fri, 2008 Jan 04, 10:51pm

Teach iTunes to Growl on Command

Update: Changed the script to make the formatting better and include the album art of the track. Thanks to Freek in the comments below for a couple of ideas. The track info is copied directly from his comments, and I didn't yet get a chance to reformat that -- maybe soon...

Haven't posted anything useful here for a while so I thought I'd put something together today and post it -- something that I've wanted for a while. Occasionally when I want to zone out at work I just put iTunes in random, hide it, and go. Unfortunately, I end up wasting time flipping iTunes open and then hiding it again when I can't remember a track name or artist or album. I've been meaning to put together something to use in Quicksilver to enable being able to take a peek at what is playing, namely using the OSX system extension Growl. Download this:

PlasticWare Downloadable ::
Growl Track Info (Oct. 21, 2007)
Download (~3,645 bytes) :: growl_track_info.zip

I can't remember why I have Growl on my system, that is, what application or system version first installed it, so if you don't have Growl, you can find it here: http://growl.info/. Basically, if you aren't familiar with it, Growl allows for (sometimes annoying) pop-up bubbles that can notify you of things going on with applications that are currently not in the foreground (for example, when an upload completes in Transmit, or when you get an incoming message in Adium or iChat).Once you've installed Growl (or check to see if it exists in your System Prefs), download the script linked above, unzip it, and place it in your iTunes scripts folder (/Users/~YourUserName/Library/iTunes/Scripts). Now, if you restart iTunes, the script should show up in your iTunes applescript menu, and you can choose the script and see that it triggers the Growl pop-up -- which really isn't terribly interesting since you are already staring at iTunes anyway.

Now, assuming that you have Quicksilver installed on your system, you can add a custom trigger as seen below.


To add the trigger, open Quicksilver and go to Catalog. Select Modules and make sure that you have the iTunes module showing there. If not, you need to enable the iTunes module. Click the circle arrow thing on the bottom right to rescan your catalog - you may notice that the count for the iTunes applescripts increases by 1. Next, select Triggers, and then select Custom Triggers. Find the little plus sign button at the middle bottom and choose HotKey. This will slide open a Quicksilver selector thing, whatever it's called. You can now type part of "growl track info" the same way as you would normally using Quicksilver, and select the Applescript "Growl Track Info.scpt" where it shows up in the list; it will default to Run as the action, which is fine. Now double click the line in the Trigger column on the right and set up your hotkey for On Press (my friends make fun of me for choosing something like Control-Option-Command-Shift-I so I can just mash the bottom left of the keyboard and one other letter and not interfere with other application keystrokes, but if you do that I won't make fun of you). You should now be able to hide iTunes and use the Quicksilver trigger to see whats playing in iTunes, as well as control iTunes with the rest of the iTunes module available for Quicksilver, and free up whatever corner of your screen you were keeping the mini player before.

11 comments Sun, 2007 Oct 21, 8:41pm

Textmate: Run Command in Terminal

Maybe this exists already - I couldn't find it, so I just wrote it. Of course, I am putting this here as code, but one day I'll publish a new bundle of TextMate commands which includes this and some of the other ones I've put together. I was looking around today for some commands that I might use to augment the Perforce bundle -- specifically, I needed a dialog. What I found was some references to the use of CocoaDialog, which is pretty handy. Another use I had envisioned for dialogs in a TextMate command was to be able to run terminal commands *in* the Terminal. After all, sometimes you would like to have something run and dump its output right there in the Terminal, rather than in a new TextMate document or something like that. The Apple Terminal is pretty nice for what it is, and its much better than the Linux KDE terminal, the Windows terminal, and the poor bastard Windows Cygwin terminal.
To set up this in TextMate, open the Bundle Editor and create a new Command. Set the command to Save "Nothing", Input "None", and Output "Discard". Give it a key command if you like, and leave the Scope Selector blank. In the Command(s) field, paste the following:

CODE:
  1. #!/bin/bash
  2.  
  3. # First use CocoaDialog to pop a dialog to get
  4. # the command you would like to run.
  5. res=$(CocoaDialog inputbox --title "Run Command in Terminal Window" --informative-text "Enter the command here:" --button1 "Okay" --button2 "Cancel")
  6.  
  7. # if the cancel button was pressed then exit
  8. [[ $(head -n1 <<<"$res") == "2" ]] && exit_discard
  9.  
  10. # set the command string to what was entered
  11. command=$(tail -n1 <<<"$res")
  12.  
  13. # create our applescript which will launch a
  14. # new Terminal window if there is not one running
  15. # and otherwise run the command we want in the
  16. # current running Terminal window
  17. aplscr="tell application \"System Events\" to set terminalRunning to (exists process \"Terminal\")
  18. if (not terminalRunning) then
  19. tell application \"Terminal\" to activate
  20. tell application \"Terminal\" to do script \"$command\"
  21. else
  22. tell application \"Terminal\" to activate
  23. tell application \"Terminal\" to do script \"$command\" in front window
  24. end if"
  25.  
  26. # execute the applescript
  27. osascript -e "$aplscr" &>/dev/null &

In a nutshell, as the comments indicate, we capture the contents of the field in the dialog that is opened, and stick them into the applescript that we want to run. The applescript part checks to see if there is already a Terminal session window open, and will run the command in the Terminal. If Terminal isn't open, then it launches it. You (or maybe me in the future) could fancy this up a bit if you wanted by having the Terminal change directory to the directory in which the file you are working on exists. Not sure that is always desirable though for my purposes.

Enjoy it. (if you're a total geek you probably will, at least in principle)

Additional work needed:
1. Escape quotes (you have to do that manually now).
2. Have Terminal spawn a new window if it is already running but has no open windows. (need to check for window count in addition to process running)

Add comment Tue, 2006 Dec 12, 11:55pm

Jslint in TextMate

The previous post detailed a way to simply run JsLint from TextMate. I put together this shell script that can be run as a command on the selected files in the project drawer and on the current document not in a project window, which is more robust than the previous script. I will try to upload a bundle for this later, but for now, you can create the command in the bundle editor yourself. Make sure that the input is none and that the output is set to the HTML option. Also, *very important*, make sure that you add the unix path to jsl to your shell variables in TextMate using the variable name "JSLINT".

CODE:
  1. if [[ -n "$TM_SELECTED_FILES" ]]
  2. then
  3.  
  4.    # THIS WILL RUN JSLINT ON THE SELECTED
  5.    # FILES IN THE PROJECT DRAWER
  6.    eval arr=("$TM_SELECTED_FILES")
  7.  
  8.    for (( i = 0; i <${#arr[@]}; i++ )); do
  9.  
  10.       filePath=${arr[$i]}
  11.       pathLen=${#filePath}-3
  12.       pathEnd=${filePath:$pathLen:3}
  13.  
  14.       if [ $pathEnd == ".js" ]
  15.          then
  16.             outputFormat='<br/><a href="txmt://open?url=file://__FILE__&line=__LINE__">__FILE__</a> (Line __LINE__):<br/>__ERROR__<br/>'
  17.             $JSLINT -process "$filePath" -output-format "$outputFormat" -context -nologo
  18.          else
  19.             outputFormat='<a href="txmt://open?url=file://__FILE__&line=__LINE__">__FILE__</a> (Line __LINE__):<br/>__ERROR__<br/><br/>'
  20.             $JSLINT -process "$filePath/*" -output-format "$outputFormat" +recurse -context -nofilelisting -nologo
  21.       fi
  22.  
  23.       echo "<br/><br/>"
  24.  
  25.    done
  26.    
  27. else
  28.    
  29.    # THIS RUNS JSLINT ON THE CURRENT FILE
  30.    filePath=$TM_FILEPATH
  31.    outputFormat='<br/><a href="txmt://open?url=file://__FILE__&line=__LINE__">__FILE__</a> (Line __LINE__):<br/>__ERROR__<br/>'
  32.    $JSLINT -process "$filePath" -output-format "$outputFormat" -context -nologo
  33.  
  34. fi

If you would like to see which files were skipped due to no error or warning, you can remove the "-nofilelisting" portion of the else statement in the for loop. I especially like the way that you can specify a URL in the html output in TextMate that will allow you to link back to documents that can be opened in TextMate, even down to the line. I'm not sure where that is referenced specifically, but its a pretty cool trick; I found that litte tidbit somewhere out there - I lost it in my browser. Anyway, the above script dumps your jslint output in the HTML output window, and clicking the links for the errors will take you to the document and line where the error was found.

Add comment Wed, 2006 Nov 15, 10:31pm

Previous Posts


Site Decryption

info = { PlaIns: "the section of the PlasticStare site, the digital external brain-repository of Ryan Todd, whose brain otherwise occupies space in San Francisco, CA", ryota: "mungified version of ryan's name", haikuBio: "robotic from birth. hears sounds - listens to music. makes pixels act." }

Calendar

July 2008
S M T W T F S
« Jun    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Posts by Month

Posts by Category