If I have a file open in an application, running the following will tell me which application has the file open. It works fine if the file name has all ascii characters. But if the file name has non-ascii characters I get an error. Does anyone know a solution? For example… I have 2 pdf files named “test.pdf” and “øtest.pdf”. I open them both in Preview.
This applescript works…
set searchTerm to "test.pdf"
set cmd to "lsof +fg | grep -i " & quoted form of searchTerm
set r to paragraphs of (do shell script cmd)
set searchTerm to "øtest.pdf"
set cmd to "lsof +fg | grep -i " & quoted form of searchTerm
set r to paragraphs of (do shell script cmd)
Any help on getting the second script to work? Notice the result from the first script. It finds the “øtest.pdf” file but the returned result shows the file path as “/Users/hmcshane/Desktop/\xc3\xb8test.pdf”, so clearly something is happening with the non-ascii character which is causing the error. I found this article talking about the do shell script command: http://developer.apple.com/technotes/tn2002/tn2065.html#TNTAG3. It mentions a conversion to UTF-8 under the question “Q: What does do shell script do with non-ASCII text (accented characters, Japanese, etc.)?”. But I don’t know what to do about this.
lsof will only directly output characters that the locale settings declare as printable (see the OUTPUT section of its manpage). The key difference between the do shell script environment and the Terminal environment is that (at a minimum) LC_CTYPE is not set for do shell script, but it is in Terminal shells.
Here are some that you can run from Terminal to see how LC_CTYPE affects lsof.
echo "$LC_CTYPE"
(unset LC_CTYPE; lsof +fg | grep test.pdf) # shows hex codes for characters that have multibyte (implies high-bit is on) UTF-8 representations
(export LC_CTYPE=UTF-8; lsof +fg | grep test.pdf) # not the same as any of the typical values, but it seems to work OK
On my system, LC_CTYPE is usually “en_US.UTF-8”. It seems that we can drop the language part and still get results though.
set searchTerm to "øtest.pdf"
set cmd to "LC_CTYPE=UTF-8 lsof +fg | grep -i " & quoted form of searchTerm
set r to paragraphs of (do shell script cmd)
If you need to run multiple, independent commands, do it like this:
set searchTerm to "øtest.pdf"
set cmd to "export LC_CTYPE=UTF-8; one_command; another_command"
set r to paragraphs of (do shell script cmd)
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 3.2.1 (4525.27.1)
Operating System: Mac OS X (10.4)
Thanks so much Chris for figuring that out for me. I had no idea about that. I have to study the lsof output type more to fully understand but the applescript works perfectly. Now I just have to check it on a non-english user’s computer just to make sure. Thank you! Thank you!
Just an update… I have checked the script on several computers now and it works in all the languages tested! This fix is going in my free program called What’s Keeping Me. You can find the program on my website if you’re interested. The update will be out next week with this fix.