Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 11-17-2002, 05:24 PM   #1
santin
Triple-A Player
 
Join Date: Jan 2002
Location: Madrid
Posts: 71
ZSH completion for open

I usually find myself launching apps form Terminal with open, so I decided to make a completion function to enable tab completion. Here it is:

_open
Code:
#compdef open

## Split words at new lines
local IFS=$IFS[3]

## Read apps from cache if needed
autoload -U update_apps
(( ${+apps} )) || [[ -f ~/.zsh_app_cache ]] || update_apps && apps=( $(<~/.zsh_app_cache) )

_arguments \
  '-a[open with specified application]:application name:( $apps )' \
  '-e[open with TexEdit]' \
  '*:files:_files'
update_apps
Code:
local app_folders

## Put your application folders here
app_folders=(/Applications /Developer/Applications ~/Applications)

## Reset applications cache
rm -f ~/.zsh_app_cache
touch ~/.zsh_app_cache

## Update the cache
for folder in $app_folders; do
  [[ -d $folder ]] && print -l $folder/**/*.app(:t:s/.app/) >> ~/.zsh_app_cache
done
The first function (_open) gives the completion for the command; the second (update_apps) looks for applications and creates a list with their names.

Mainly I see two problems, the first, is that I couldn`t figure out a better way to get a list with all the applications and in fact, it only gets the applications which are bundled (.app); the second is that updating the application cache is sloooow!!!

Hope you like it.

Any improvements or comments will be greatly appreciated.

Saludos.
santin is offline   Reply With Quote
Old 11-17-2002, 06:51 PM   #2
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
Hi Santin,

nice work, and I know exactly what you mean about the slow cache updating. Very frustrating when you realise that the OS has some way of getting at this (extended) list of applications; "open" is virtually instant, so there's no traversal happening when it's invoked.

I have dribbled a few comments about finding more than just ".app" applications in another thread here. In particular, using applescript turned out to be reasonably helpful and not any more expensive than doing the required "find/xargs blah blah" routine. Trouble with the ".app" mechanism is that some carbon applications don't follow this naming convention, but rather have an APPL signature instead (OK, an APPL *type*). Notable among these are the Microsoft Office applications and BBEdit, though doubtless there are plenty of others too.

(For what it's worth, a "find/xargs" version of your updating script would probably be a lot quicker than relying on the globbing operator in zsh. It would also be significantly uglier, so it's all a juggling act.)

Unfortunately the thread referred to above is a bit of a monster, but you might find something interesting in there. (It's the "Davros" thread for those who'd like to avoid seeing that thing *yet again*: guess I really should learn to use links properly instead of just bracketing sentences like this one :-) )

http://forums.macosxhints.com/showth...&threadid=5915

Cheers,
Paul

Last edited by pmccann; 11-17-2002 at 06:53 PM.
pmccann is offline   Reply With Quote
Old 11-17-2002, 07:07 PM   #3
santin
Triple-A Player
 
Join Date: Jan 2002
Location: Madrid
Posts: 71
Thanks Paul, I'll take a look to that monster when I get enough time.

In fact, most of my apps are bundled, so I don't really need to get the others, and now BBEdit 7 comes bundled so that leaves us M$ Office... I'm glad I keep my computer M$ free .
santin is offline   Reply With Quote
Old 11-17-2002, 07:35 PM   #4
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
Yep, most are app'd; a quick glance through my /Applications directory showed a couple of other candidates (FWIW): graphicconverter, R. Not that much.

(OfficeX isn't bad at all, but there's no way I'd touch it with a bargepole if I had to pay retail price! It's one of the few edu perks that's worthwhile; university pays a considerable microsoft tax per head of staff, and said staff get all the standard bits and pieces free for work and home. For mac users that's none too exciting: officeX is pretty much it. PC users deserve all the microsoft applications that they get!)

Cheers,
Paul
pmccann is offline   Reply With Quote
Old 11-19-2002, 04:37 AM   #5
vonleigh
All Star
 
Join Date: Jan 2002
Posts: 579
Simple zsh question (since I think we're following the same thread), how do I use the kleene star?

Say I want to do:

Code:
% rm -r .Trash/*
Uhm.. wait... it actually worked and asked me:

zsh: sure you want to delete all the files in /Users/vonleigh/.Trash [yn]?

*scrolls up to check terminal*

Ah, here we go, when I did:

Code:
% sudo rm -r .Trash/*
zsh: no matches found: .Trash/*
So how would i do it so it works? I tried escaping the star \:

Code:
% sudo rm -r .Trash/\*
Password:
rm: .Trash/*: No such file or directory
But that didn't quite work.


v


P.D: Please check my netinfo Password? post. I'm trying to merge from old to new OS installation and I'd like to know if that approach will work.

I've looked up info on the encryption of passwords, but so far none have been able to tell me about what they depend on for consistency (aka. what do they depend on so they translate to the same value).
vonleigh is offline   Reply With Quote
Old 11-19-2002, 04:09 PM   #6
Glanz
Major Leaguer
 
Join Date: Aug 2002
Location: Montreal
Posts: 373
Great stuff!!! as an old Z fan, I appreciate that! I find your codeing a refresher course
Glanz is offline   Reply With Quote
Old 11-19-2002, 04:22 PM   #7
osxpez
Major Leaguer
 
Join Date: May 2002
Location: Sweden
Posts: 282
vonleigh: The wildcard star must be unsecapde to work its magic. The reason that first sudo command didn't work must be that you either were not standing in your home directory or there were no files in the Trash.
__________________
/PEZ
osxpez is offline   Reply With Quote
Old 11-19-2002, 05:58 PM   #8
santin
Triple-A Player
 
Join Date: Jan 2002
Location: Madrid
Posts: 71
After some testing, I found a little mistake in the _open function, so here is an updated version:

_open
Code:
#compdef open

## Split words at new lines
local IFS=$IFS[3]

## Read apps from cache if needed
autoload -U update_apps
if (( ! ${+apps} )); then
  ( [[ -f ~/.zsh_app_cache ]] || update_apps ) && apps=( $(<~/.zsh_app_cache) )
fi

_arguments \
  '-a[open with specified application]:application name:( $apps )' \
  '-e[open with TexEdit]' \
  '*:files:_files'
If you are interested, it had to do with the deletion of the cache file after calling the _open function.

Still looking for a good way to get the list of known apps, I think I'll be coding a foundation tool to scan for applications and create the cache, it should be faster than the applescript part suggested by Paul in "Davros".
santin is offline   Reply With Quote
Old 11-19-2002, 07:07 PM   #9
vonleigh
All Star
 
Join Date: Jan 2002
Posts: 579
It seems theres:

_LSCopyAllApplicationURLs

But it's private unfortunately. Maybe you can figure it out. If not you could scan for the type AAPL and .app and see if that works.



v
vonleigh is offline   Reply With Quote
Old 12-07-2002, 03:02 PM   #10
santin
Triple-A Player
 
Join Date: Jan 2002
Location: Madrid
Posts: 71
Talking

Sorry for taking so long without giving feedback, but i've been on holidays for a week .
Besides, I've done a full reinstall of two of my machines at home, and one was a debian PC.

I finally managed to code the tool, it is still a bit dirty (as it is my first Cocoa experience), but it works; it recursively scans /Applications, /Developer/Applications and ~/Applications looking for apps, I will upload it tomorrow when I get to my TiBook... (I'm currently at my debian box).

Hasta pronto.
santin is offline   Reply With Quote
Old 12-08-2002, 03:42 AM   #11
santin
Triple-A Player
 
Join Date: Jan 2002
Location: Madrid
Posts: 71
Now, here's the link to the tool: lka tool
It is a project for PB, so you need it to compile. After you compile it place lka somewhere in your path (~/bin). Of course, feel free to take a look at the source code and customize it if you like... sorry it is not documented at all , but it is quite simple to see what it does...

Oh, and here is a modified version of the update_apps function:
update_apps
Code:
local app_folders

## Put your application folders here
app_folders=(/Applications /Developer/Applications ~/Applications)

## Reset applications cache
rm -f ~/.zsh_app_cache && touch ~/.zsh_app_cache && \
  chmod go-rwx ~/.zsh_app_cache

## if lka tool present, use lka tool
## for now, lka doesn't take $app_folders into account
if ( which lka >& /dev/null ); then
  lka >> ~/.zsh_app_cache
## if not present, use alternative way (slower and not complete)
else
  ## Update the cache
  for folder in $app_folders; do
    [[ -d $folder ]] && find $folder -name "*.app" | \
      sed -e 's?.*/\(.*\)\.app?\1?g' >> ~/.zsh_app_cache
  done
fi
After some testing, lka takes 5-10 seconds to get the list, while find takes about 25-40 and the old print ** method takes more than 40, so the improvement is noticeable...

If you have any comment about the tool or would like a further explanation about the code feel free to post it here... I know it's easy to improve it but I'm just so laaazyyy

Hope you enjoy it.

[edit: readability -mt]

Last edited by mervTormel; 12-08-2002 at 11:40 AM.
santin is offline   Reply With Quote
Old 02-08-2003, 05:33 AM   #12
santin
Triple-A Player
 
Join Date: Jan 2002
Location: Madrid
Posts: 71
A few days ago I uploaded an updated version of lka tool, but I've had no time to post it here till now.
It features command line options an adds a depth limit, so you can control how deep you want it to go into folders and increase performance (for example, with a limit of 3, it finds (and prints) most apps I have in about 2 seconds..., in fact comparing with the full list of apps without limit, I can't see an app I would use).

Again, it's a PB project and you can find it here.

It also includes _open and update_apps functions for zsh.

Saludos.
santin is offline   Reply With Quote
Old 02-14-2003, 01:32 AM   #13
Titanium Man
Guest
 
Posts: n/a
Thanks for your work, santin. I put the functions in my $fpath as per the README file (I put them in ~/.zfuncs to be precise), then I compiled lka and placed it in ~/bin. I opened a new term window, and then...nothing. How does this work? Does one type "open TextEdit" or "TextEdit" or "textedit" or "open textedit" or what? I tried all of these things and nothing worked. I have

autoload -U +X ${fpath[1]}/*(:t)

in ~/.zshenv if that makes any difference. Thanks.
  Reply With Quote
Old 02-14-2003, 06:52 PM   #14
santin
Triple-A Player
 
Join Date: Jan 2002
Location: Madrid
Posts: 71
TiMan, you simply need to type "open -a" and then the name of the application, for example:
Code:
open -a TextEdit
The functions simply allow you to type "open -a" and then use TAB for completion of the application names (as if they were normal commands):
Code:
open -a text<TAB>
Of course, it depends on how many apps you have with similar names and how you have set up your completion system (case [in]sensitivity)...

You don't really need to autoload the functions provided that you autoload compinit in your .zshrc

The first time you try to complete an app for the open command (with TAB) it will create an application cache. The completion function for open is _open and it uses update_apps to create the cache. update_apps calls lka and redirects its output to the file that will store the application cache (it will take a few seconds depending on your system and number of apps).
After the application cache is created, app completion will be really fast.

If you later want to update the application cache (for example if you install a new application), you can use the update_function directly. You can also create an script to update the cache and put it in your crontab.

Hope this explanation helps...
santin is offline   Reply With Quote
Old 02-15-2003, 10:56 AM   #15
Titanium Man
Guest
 
Posts: n/a
Thanks for the explanation, santin. I wish I could get this to work on my system. I put the functions in ~/.zfuncs (which is part of my $fpath), and put lka in ~/bin. (I built lka in Project Builder first). I opened a new term window, and typed 'open -a Text<tab>' and nothing happened. I then typed 'update-apps' and the file .zsh_app_cache did appear, but I still didn't get tab completion. I did 'less .zsh_app_cache', and it said that the file might be a binary file, do I wish to see it anyway. So I said yes, and removed the line for BBEdit which had a non-printing character in it oddly enough. But still no tab completion. I did 'less _open' and 'less update_apps' and was told that they were binary files too. That went away when I edited the functions and changed the accented i in Santin to an unaccented i and the accented A in Alvarez to an unaccented A. Still, no tab completion. I'm puzzled. If you have any ideas, I'd love to hear them, although I understand if you can't spend all of your time giving tech support on a free app ;-) Saludos.
  Reply With Quote
Old 02-15-2003, 06:45 PM   #16
santin
Triple-A Player
 
Join Date: Jan 2002
Location: Madrid
Posts: 71
Make sure you have the following in your .zshrc
Code:
autoload -U compinit
compinit -C
That should make the completion available, if not...
It seems update_apps works (the cache is created), and that means lka works, so
I donīt know, maybe it's got to do with the environment, in fact I have a similar setup (~/.zfunc ~/bin).

Sorry about the accents though, I pasted that snippet and it seems it got in UTF8...

I've just remembered something, if you type
Code:
open -<TAB>
it should display the available options:
Code:
-a -- open with specified application
-e -- open with TexEdit
try it and see if it works

Last edited by santin; 02-15-2003 at 06:51 PM.
santin is offline   Reply With Quote
Old 02-16-2003, 04:27 AM   #17
santin
Triple-A Player
 
Join Date: Jan 2002
Location: Madrid
Posts: 71
I've found the problem (and it's got nothing to do with the environment as I suspected), it seems that completion functions must start by #compdef, so the comments are causing trouble.

It was working in my system because of the dump compinit creates, as it is only updated if you add new completion functions, and I had the function before pasting the comment so the dump was outdated.

So a fast solution to your problem is to delete those initial comments in _open, delete the dump from compinit (usually ~/.zcompdump) and open a new terminal.

If you redownload the file, you will get the updates and a README with some examples.

Thanks TiMan for discovering the bug...
santin is offline   Reply With Quote
Old 02-16-2003, 10:51 AM   #18
Titanium Man
Guest
 
Posts: n/a
That was it! I was just about to give up. I had started with fresh .zsh* files with nothing in them except "autoload -U compinit" and "compinit -C" in order to eliminate any environmental cause, but it still wasn't working. However, after following the instructions above, everything now works as expected. So now I have two ways to make my life easier (lazier): opena from pmccann and lka from santin. Thank you both, and thanks santin for your persistence in solving my problem.
  Reply With Quote
Old 03-27-2004, 08:25 PM   #19
wgscott
Major Leaguer
 
Join Date: Sep 2002
Posts: 350
this is faster and includes carbon apps without .app

Here's another attempt:

http://chemistry.ucsc.edu/~wgscott/xtal/AppGetter.zsh

This gets your applications by running a script first and it stores them in a db dot file in your home directory (you might prefer /var/db -- edit to suit your needs). You can update this daily with cron if you want, or run it manually.

The last three lines, when uncommented and placed into your compctl file, then load the completion more or less instantly.

There might be a prettier way, but it works. Please feel free to make it better.
wgscott is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:35 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.