Posts Tagged ‘MacOS’

AppleScript to Re-Apply Finder Comments

Monday, January 25th, 2010

Finder Comments Lost

When restoring from a backup, depending on the Mac-specific intelligence of your backup solution, or when copying files written by a 10.4 Mac, your Spotlight (Finder) Comments stored in the .DS_Store files might not survive the round trip. You’ll first notice this by the fact that your comments field or column is completely empty for files you know you previously tagged or made comments on.

Leopard, Snow Leopard Comment Storage

Tiger and previous editions of Mac OSX store Spotlight comments in the .DS_Store file exclusively. Leopard and Snow Leopard on the other hand, claim to maintain backwards compatibility by storing the Spotlight Comments in both the .DS_Store and the new Extended File Attributes. I question this thinking though, because Mac OSX developer Steve Gehrman of the awesome PathFinder team says that Finder, while it writes both formats, still only reads back the .DS_Store ones. It seems to me that Apple would have changed Finder to read from the newer Extended Attributes as soon as they started writing to those in duplicate.

“On Mac OS X 10.4 Tiger, for example, .DS_Store files also contain the Spotlight comments of all the folder’s files, whereas Mac OS X 10.5 “Leopard” stores this information in Extended file attributes.”

Script Research

Giving a tip of the hat to the similar-but-not-quite-what-I-wanted script that helped me get enough of the syntax working (using type alias instead of type file was tricky) get my own authored, I give you:

A MacOSXHints article, and the corresponding code

The Solution

To solve this extended attributes vs. .DS_Store discrepancy, we only need to read (from the extended attributes) and reapply (thereby recreating the .DS_Store) the same comment. The solution is this AppleScript. Just highlight the files needing the treatment in Finder, then execute this script from the AppleScript Editor.

The result is that Finder (which reads only the .DS_Store files) and PathFinder (which only reads the extended attributes) can both now see the Spotlight Comments.

Extras

If you would like your Mac to automatically clean up the .DS_Store files it writes out to flash sticks and network drives, check out BlueHarvest, an interesting little utility app that fills this need.

Java 1.6 Memory Tools on the Mac, Part II

Saturday, July 11th, 2009

I’ve been battling getting jmap working properly on a new Mac. I followed the invaluable instructions my NFJS colleague, Ken Sipe, provided for the Rhino engine inclusion. However, I was still encountering an odd issue where I couldn’t get jmap to attach to a Java process.

First, I would start a simple helloworld Java application.

java com.ambientideas.HelloWorldJava
Hello Java World!

Picture 1.png

Then I would get the PID:

jps
11554 HelloWorldJava
11573 Jps

Then I tried to jmap it, but got the following error

jmap -heap 11554
Attaching to process ID 11791, please wait...
attach: task_for_pid(11791) failed (5)
Error attaching to process: Error attaching to process, or no such process

So I thought, “These memory tools often require root access, so I’ll sudo it.” When I did, I got the following equally cryptic error:

sudo jmap -heap 11791
Attaching to process ID 11791, please wait...
Exception in thread main java.lang.RuntimeException: gHotSpotVMTypes was not initialized properly in the remote process; can not continue
at sun.jvm.hotspot.HotSpotTypeDataBase.readVMTypes(HotSpotTypeDataBase.java:111)
at sun.jvm.hotspot.HotSpotTypeDataBase.<init>(HotSpotTypeDataBase.java:68)
at sun.jvm.hotspot.MacOSXTypeDataBase.<init>(MacOSXTypeDataBase.java:35)
at sun.jvm.hotspot.bugspot.BugSpotAgent.setupVM(BugSpotAgent.java:560)
at sun.jvm.hotspot.bugspot.BugSpotAgent.go(BugSpotAgent.java:481)
at sun.jvm.hotspot.bugspot.BugSpotAgent.attach(BugSpotAgent.java:319)
at sun.jvm.hotspot.tools.Tool.start(Tool.java:146)
at sun.jvm.hotspot.tools.JMap.main(JMap.java:128)

I though, “Hmmmm, this looks a lot like a java version mismatch. Let’s check which java version I’m running.” I had javac compiled the HelloWorld against Java 6, so was expecting a matching java. I made a quick stop into the Java Preferences pane. Yup. Java 6 set as the first-priority choice.

Picture 2.png

Let’s check the command line too…

java -version
java version 1.6.0_13
Java(TM) SE Runtime Environment (build 1.6.0_13-b03-211)
Java HotSpot(TM) 64-Bit Server VM (build 11.3-b02-83, mixed mode)

Excellent. Just what I expected. Java 6. But a little doubt crept into my mind. What if the sudo’ed invocation were finding a different Java. That didn’t sound plausible, but yet was still worth checking out.

sudo java -version
java version 1.5.0_19
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_19-b02-304)
Java HotSpot(TM) Client VM (build 1.5.0_19-137, mixed mode, sharing)

Aha! There’s the culprit. So somehow, sudo’ed commands are running Java 5, even though I’ve set a preference for 6 in my profile. I searched the web high and low for a way to specify the Java version for “sudo” specifically, but could find no trace. I even tried the -E flag to maintain the caller’s environment variables, as is possible on Linux distributions. Not implemented on Mac.

I did a quick check of my path and a “which java”. Both ultimately routed to Java 6. I also had JAVA_HOME and JAVA_VERSION set just fine.

set | grep JAVA
JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
JAVA_VERSION=1.6

echo $PATH
/Applications/Dev/btrace/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:
/usr/X11/bin:./:
/Users/mccm06/scripts:/Applications/Dev/apache-ant/bin:
/Applications/Dev/apache-maven/bin

So I turned to harness the wisdom of one of the really smart guys (that uses a Mac) I’m privileged to hang out with in Denver, Fred Jean. Fred said, “Try adding the 1.6 java explicitly to the front of your path rather than letting Java Preferences pane be the sole controller.” I opened up ~/.bash_profile (alternatively you could edit ~/.bash_rc)and slammed in the following:

export PATH=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Commands:$PATH

I opened a new terminal to pick up the shell changes. Then I started checking if this fixed the issue in reverse order, starting with the sudo:

sudo java -version
java version 1.6.0_13
Java(TM) SE Runtime Environment (build 1.6.0_13-b03-211)
Java HotSpot(TM) 64-Bit Server VM (build 11.3-b02-83, mixed mode)

200907110802.jpg

Looking good! Now let’s get back to our core goal, jmap.

200907110804.jpg

sudo jmap -heap 5951
Attaching to process ID 5951, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 11.3-b02-83
using parallel threads in the new generation.
using thread-local object allocation.
Concurrent Mark-Sweep GC
Heap Configuration:
    MinHeapFreeRatio = 40
    MaxHeapFreeRatio = 70
    MaxHeapSize = 88080384 (84.0MB)
    NewSize = 21757952 (20.75MB)
    MaxNewSize = 43581440 (41.5625MB)
    OldSize = 64159744 (61.1875MB)
    NewRatio = 7
    SurvivorRatio = 6
    PermSize = 21757952 (20.75MB)
    MaxPermSize = 88080384 (84.0MB)
Heap Usage:
New Generation (Eden + 1 Survivor Space):
    capacity = 19070976 (18.1875MB)
    used = 2351616 (2.24267578125MB)
    free = 16719360 (15.94482421875MB)
    12.330863402061855% used
Eden Space:
    capacity = 16384000 (15.625MB)
    used = 2351616 (2.24267578125MB)
    free = 14032384 (13.38232421875MB)
    14.353125% used
From Space:
    capacity = 2686976 (2.5625MB)
    used = 0 (0.0MB)
    free = 2686976 (2.5625MB)
    0.0% used
To Space:
    capacity = 2686976 (2.5625MB)
    used = 0 (0.0MB)
    free = 2686976 (2.5625MB)
    0.0% used
concurrent mark-sweep generation:
    capacity = 64159744 (61.1875MB)
    used = 0 (0.0MB)
    free = 64159744 (61.1875MB)
    0.0% used
Perm Generation:
    capacity = 21757952 (20.75MB)
    used = 4716880 (4.4983673095703125MB)
    free = 17041072 (16.251632690429688MB)
    21.678878600338855% used

Thanks to a combination of Ken Sipe, Fred Jean, a few good guesses and some investigative work, the problem is solved! I’m longwindedly calling this the “java version is different for sudo-initiated processes on Mac” so that the next person Googling for this using similar keywords will hopefully find this tutorial.

As a closing note, I often get asked how I set my environment variables for Java so that Spotlight-launched, Quicksilver-launched, Dock-launched, and Terminal-launched processes can all see the variables (yes, there’s some odd pitfalls about that on a Mac due to the process parents being different). The short answer is to create a text file called /private/etc/launchd.conf and put the following (applicable portions, based on your tool usage) contents in it:

setenv JAVA_VERSION 1.6
# A) Some process-launchers will cause their child to default to 1.5 if using /Library/Java/Home
# even if Java Preferences pane says 1.6 at the top
# setenv JAVA_HOME /Library/Java/Home
# B) Alternate: Force Java 1.6 as Java Home
setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
setenv GROOVY_HOME /Applications/Dev/groovy
setenv GRAILS_HOME /Applications/Dev/grails
setenv NEXUS_HOME /Applications/Dev/nexus/nexus-webapp
setenv JRUBY_HOME /Applications/Dev/jruby
# Set up Ant
setenv ANT_HOME /Applications/Dev/apache-ant
setenv ANT_OPTS -Xmx512M
# Set up Maven
setenv MAVEN_OPTS -Xmx1024M
setenv M2_HOME /Applications/Dev/apache-maven

As we have learned the hard way, the only pitfall is that the java version specified in launchd.conf is not honored by sudo’ed java invocations.

Drop me an email if you’ve seen this issue and this tutorial helps you solve it.

iTerm Screencast

Tuesday, February 17th, 2009

I’m a big fan of iTerm as a replacement for Terminal.app for developers. It offers a host of productivity boosts. To name a few:

  1. Named tabs
  2. Favorite location bookmarks
  3. Window, font, and background colors per-bookmark
  4. Transparency levels and blur of the underlying windows
  5. Middle click copy and paste
  6. URL launching from any URL recognized text

I’ve put together a quick screencast to show off a number of these features and how to configure them.



iTerm Developer Tips from Matthew McCullough on Vimeo.

iPhone SDK, Cocoa & RESTful Web Services, Memory Leak

Friday, January 16th, 2009

Recently, I gave the second version of my iPhone and Java Web Services talk at the Boulder JUG. It was a great audience filled with interest and great questions. I promised them I would continue to load up my Delicious bookmarks with great iPhone links, and I’m doing just that.

Pertinent to that talk, let’s quickly revisit that memory leak issue for NSURLConnection. In short, if you call sendSynchronousRequest, you get an internal memory leak of 128 bytes of a NSCFString object from inside the API.

To isolate this from my application coding skills via the iPhone and Java Web Services demo code, let’s look at an example called ZipWeather from AppsAmuck. Attach the profile to the ZipWeather, exactly as downloaded. Run it. Type a zip code. It leaks.

It appears that the NSURLConnection:sendSynchronousRequest() Flaking out, even for others, calling this API to Amazon Web Services. I believe it may be the leak contributing to these hiccups. I’ve tried turning off the cache, but it still leaks. I’ve tried the async version and it still leaks too.

This Apple article even suggests using this same API in the same way that ZipWeather and my iPhone and Java Web Services app does.

In short, I’m submitting another Radar report to Apple about this and hope it doesn’t get closed out as “Unable to reproduce” as João Pavão’s defect # 6179277 did. I’m able to reproduce it every time, with everyone’s sendSynchronousRequest calls.

I love the platform, but as you all know, one core API bug can really cause a lot of challenges until resolved. Let’s hope this one gets resolved very soon!

References:

  1. SOAP Service Calls on the iPhone
  2. Stack Overflow thread on Cocoa Web Service Access
  3. Apple iPhone Dev Center
  4. A Blog about 5 languages calling web services, including Objective-C
  5. A continuation blog post specifically on Objective-C and REST
  6. And a download of a sample client in Objective-C that calls REST methods
  7. UPDATE: SeismicXML leak discussion
  8. UPDATE: MacRumors sendSynchronousRequest leak discussion
  9. UPDATE: NSXMLParser leaks too

200901161532.jpg

MacBook Pro (late 2008) “Hot Laptop In Bag” Troubles

Wednesday, December 10th, 2008

As an owner of two “MacBook Pro (late 2008)” units (gotta love the Apple official model title), I’ve experienced an overheating issue quite frequently. It’s becoming known as “hot laptop in bag, syndrome, v2″. Here’s a quick tour of the problem with a bunch of the news articles published in just the last few days.

There’s a quick description of the problem by Gizmodo then, the actual thread on the Apple site, followed by a suggestion to use SmartSleep to turn on Hibernate mode only, or reset your SMC, or reset your PRAM, or actually get a firmware fix. Take your pick.

And if you don’t find that enough, go ahead and check out the electron microscope pictures and spectrograph of the bump composition that says NVidia is not being transparent about the failing video chip problem.

Does this just make you want to run out and buy one of these new units?

Lest you think I’m switching back to PCs, these Macs are still way better than my last Dell D600 that required 4 complete overhauls for failed components in a 90 day period.

Verizon Pantech UM175 Mobile Wireless Card on Mac OS 10.5 with Native Drivers

Friday, October 17th, 2008

Apple has a discussion forum going on this very card, but I couldn’t make heads or tails of which was actually the right solution. So, I present to you here, screenshots and a walkthrough of the setup.

1) Purchase a Verizon Pantech UM175, one of the latest USB wireless cards from Verizon. Should be able to get it for $0 with a 2 year contract.

2) Plug it in. Mac OSX 10.5 Leopard will detect it as a modem

3) Change the settings per the screenshots below
3.a) Vendor: Sierra, Model: CDMA, Dial Mode: Ignore dial tone when dialing
3.b) Telephone Number: #777, Account Name: YOURNUMBER@vzw3g.com, Password: VZW

Verizon Wireless Card Settings 2.png Verizon Wireless Card Settings 1.png

Setting up Sun’s VisualVM on Mac OSX

Monday, September 8th, 2008

There is a relatively new tool out from Sun call the VisualVM. It is, in short a super new version of JConsole. In fact, it even runs all the extensions you have previously written for JConsole. Nice job Sun! You can profile, take snapshots, and watch in real time, threads, memory usage, and so much more of any local or remote java application.

Now, it takes a little bit of a trick to get it to work on Mac OSX. You need the latest Java 6 JDK installed, though it can monitor apps running on JRE 1.4 through JRE 7.0. But if you don’t set it as your default JDK, which can cause many apps such as Eclipse and CyberDuck to stop working, then you’ll need to use the –jdkhome option when launching visualvm. I set up a shell script to do so. The full invocation is as follows:

visualvm --jdkhome /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/

There is even a getting started guide that shows you the basic features. And if video is more your style, there’s a vodcast that shows off this new app as well. If you still can’t get enough of this new tool, there’s a neat DZone overview written by Geertjan Wielenga.

How To Remove Stuck Sidebar Items in MacOS

Sunday, February 10th, 2008

Well, here’s one of those things that isn’t Mac-easy on Mac OS. Say you create a sidebar link to a server. Say you no longer have access to that server. Whoa. Can’t click on it and remove it. Just keeps popping up an error message. Well, it turns out you have to follow these instructions and edit the ~/Library/Preferences/com.apple.sidebarlists.plist file to purge the offending link. Come on! You have to be kidding Steve, right?