Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use a free program to simplify dragging from Terminal UNIX
For a long time I've really liked that you can drag files from the finder (or elsewhere) to a Terminal window, where they appear as a path. Unfortunately, there has never been a nice way to drag from the Terminal. For example, if I have a new tarball that I want to send someone, I have to click Attach in Mail, and then find the file using the Open sheet.

I've just written a program -- Drag.app -- that does allow you to drag from Terminal, and it is available for free, with source. It adds a new command (shell script), drag , which is usable from Terminal. When executed, it opens a new window with a file icon that can be dragged as normal. When the drag is complete, the app quits so your desktop isn't cluttered and you can get back to the command line.

The closest I've seen to this before is the open command. If you open the enclosing directory of a file, then it will be displayed in the Finder -- you can find and drag a file from there. That approach has the downside that you'll sometimes get a .DS_Store file, and it can still be hard to find the file you want if the directory has a lot of files in it. My command cuts to the chase and lets you drag the file(s) you want, then gets out of your way.

[robg adds: I tried this, and it worked well. The download includes a compiled Intel Mac version; you'll have to compile your own for PowerPC, and that's left as an exercise to the reader...]
    •    
  • Currently 3.20 / 5
  You rated: 4 / 5 (5 votes cast)
 
[7,591 views]  

Use a free program to simplify dragging from Terminal | 27 comments | Create New Account
Click here to return to the 'Use a free program to simplify dragging from Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Even simpler
Authored by: SOX on Aug 29, '07 08:31:35AM
just type:
open ./
and the finder opens a window on the current folder in terminal.

to open a finder window to the folder containing an arbitrary file type:

open `dirname /the/path/to/some/file`

or even simpler, delete the filename from the end of the path ad write:

open /the/path/


[ Reply to This | # ]
Even simpler
Authored by: gteague on Aug 29, '07 07:55:13PM

marsneedswomen

serious as a heart attack. some mac app (bbedit?) had this in their pkg/bundle file and i've been using it up until i did a clean install last year. not surprisingly, i renamed it to 'dragit' for actual use.

unfortunately, i just lately zapped a partition holding some old apps, so i may never remember what developer included this handy terminal app with some unrelated application.

/guy

---
--
Watch out where the huskies go--don't you eat that yellow snow. F. Zappa



[ Reply to This | # ]
Even simpler
Authored by: gteague on Aug 29, '07 07:57:38PM

very sorry. i see smyklar beat me to it with his link. i'll bet i found out about marsneedswomen from the osxhints link he provided.

at least 2 of us have been around a long time. and i'm still using max menus.

/guy

---
--
Watch out where the huskies go--don't you eat that yellow snow. F. Zappa



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: forman on Aug 29, '07 08:41:42AM
&uotThis is an interesting way of making easily accessible data on the command line easily accessible in the GUI. I'll give it a try. Currently, after working in a given directory on the command line, to then access the files in the GUI quickly, I simply type:

open .

Where "." is the Unix shorthand for "the current directory". The command "open ." has now become one of my top used commands.

While this technique is quick and built into MacOS, it has the disadvantage of using "open; which does different things depending on the filetype of the arguments passed. I like your solution, in that it provides a single function, operating on filenames instead of directories and can thus be used easily with other command-line programs such as xargs.

[ Reply to This | # ]

Use a free program to simplify dragging from Terminal
Authored by: Soliman on Aug 29, '07 09:02:33AM
As usual Quicksilver is your friend, just type in qs file (if you chose to install the CL plugin), or call quicksilver and type in the name of your file, or get to it by QS's browsing mode, and select the action Reveal in finder (default shortcut is Ctrl-return) and there you go ! :)

---
Sylvain

[ Reply to This | # ]

Use a free program to simplify dragging from Terminal
Authored by: Soliman on Aug 29, '07 09:04:33AM

Of course this is Command-return and not Ctrl-Return... :(


---
Sylvain



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: thejoecarroll on Aug 30, '07 01:12:11AM

Why even use the "Reveal in Finder" command when the mighty QuickSilver supports direct dragging and dropping both into and out of it's command interface window's object selection panel?
:-)

BTW, the <i>qs</i> command-line tool, of course, also works with multiple file/folder selections.



[ Reply to This | # ]
Simpler but not as simple as the first hint
Authored by: Bemopolis on Aug 29, '07 09:11:18AM
One way to send a file is to type

open -a Mail /path/to/file

This opens a new Mail message window with the file already attached.

For those as lazy as I am, you can create an alias in your .*rc for the open -a Mail part. In my case I send /path/to/file

[ Reply to This | # ]
Re: Simpler but not as simple as the first hint
Authored by: leamanc on Aug 29, '07 06:35:18PM

We can rate the hints now, but I wish we could rate the comments too. The open -a Mail tip is great! To me, this is more intuitive than the Drag.app of the hint.

Good job!



[ Reply to This | # ]
Re: Simpler but not as simple as the first hint
Authored by: Uncle Asad on Aug 30, '07 08:26:00PM

open -a works for Eudora, too.



[ Reply to This | # ]
Re: Simpler but not as simple as the first hint
Authored by: klktrk on Aug 30, '07 11:31:01PM

Lovely! Just added the alias to my .bash_profile, and I have much command line joy.

---
The Apotek
http://theapotek.com
The Executioner's Summary:
http://www.last.fm/label/Broken+Hill+Music/playlists/6761?autostart



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: smyklar on Aug 29, '07 01:19:50PM
You mean like this?
[link:]http://www.macosxhints.com/article.php?story=20021220062633569&query=drg

This rang a bell immediately with me, because I use this nearly identical function in my own terminal work from time to time. Very useful - and PPC native to boot.




[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: jaguarcy on Aug 29, '07 03:20:24PM

2 problems with this app:

1) Doesn't handle spaces / special characters in a file name,

2007-08-30 01:18:22.603 Drag[22954] Couldn't convert path "/Users/admin/Documents/Documents" to an FSRef to put on the pasteboard.
2007-08-30 01:18:22.603 Drag[22954] Couldn't convert path "/Users/admin/Documents/Pro" to an FSRef to put on the pasteboard.
2007-08-30 01:18:22.603 Drag[22954] Couldn't convert path "/Users/admin/Documents/Cut" to an FSRef to put on the pasteboard.
2007-08-30 01:18:22.603 Drag[22954] Couldn't convert path "/Users/admin/Documents/Final" to an FSRef to put on the pasteboard.


2) the drag window opens up UNDER the terminal window, requiring extra work to bring focus to the drag window.



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: rdj on Aug 30, '07 07:42:02AM
The simplest fix for this is to modify the "drag" shell script to read:

exec /Applications/Drag.app/Contents/MacOS/Drag "$@"

Note the double-quotes around "$@".

This is important for anyone who writes Unix shell scripts: you must be careful about the effects of quoting after substitutions. "$*" would not be correct in this case, for example, because it would result in a single quoted token with all of the substituted parameters included in the one token, separated by a space (technically, the IFS setting.)

Anyway, using "$@" takes care of the blanks-in-file-specifications issue.

-^-rdj-^-

[ Reply to This | # ]

Use a free program to simplify dragging from Terminal
Authored by: wgscott on Aug 29, '07 03:31:57PM
Here is a shell script that does the same (and handles spaces). The Finder purposely does not come into focus, but if you just put the line "open -a Finder" at the end, it will pop to the front.

[ Reply to This | # ]
Nice programming
Authored by: klktrk on Aug 30, '07 11:34:40PM

Really nice script. Great programming style, and nicely thought out.

...

But zsh? :-)



---
The Apotek
http://theapotek.com
The Executioner's Summary:
http://www.last.fm/label/Broken+Hill+Music/playlists/6761?autostart



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: rogov on Aug 29, '07 04:08:56PM

No, this is actually pretty slick.

I am a voracious user of 'open', but it has limitations. Also people suggesting 'open .' ignore that this will create a .DS_Store file in the directory which this hint implies that Drag.app avoids. Toolchain support to avoid unintentional .DS_Store creation can be very handy at times.

One example where 'open -a' is limited is when you need to drag something into an application, but the operation is more contextual than just dropping anywhere. For example, when an application opens a file dialog to select a file, sometimes it's convenient to drag a file onto the dialog directly rather than traversing the file tree or searching for it. Another example is that I suspect 'open -a' wouldn't do what you hope for dragging into drop zones like iDVD and iMovie have.


The selectfile script opens a Finder window, thus creating the .DS_Store. Also, the selectfile script seems to fail for me with multiple files listed.

Don't know about QuickSilver.


It would be good for this program to handle spaces and special characters though. And another feature which may not be possible would be to support undo for when you drag to relocate files.






[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: rogov on Aug 29, '07 05:16:09PM

Actually, I just tried paths and file names with spaces for Drag.app and it does work for me.



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: rogov on Aug 29, '07 05:44:23PM

Oh, I see. The actual Drag.app works fine if you directly invoke it:
/Applications/Drag.app/Contents/MacOS/Drag Some\ Dir\Some\ File.txt

But if you use the drag shell script to invoke it, the shell script seems to mangle the argument string. I'm not sure how to fix the shell script though.



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: Lliwynd on Aug 29, '07 08:50:08PM

Hi all,

Yes, as I noted in the original submission text, you can use 'open'. As I and others have also noted, this has its own problems.

I didn't know about marsneedswomen. I was amazed that something like this didn't already exist. Now I know that it did, but I didn't find it. My app still has the advantage of being free/open source. And this hint exists as a forum for duscussion of other solutions.

Regarding the 'spaces in filenames' issue. I don't want to turn MacOSXhints into a support forum. I've already had someone send me patches, so hopefully that will work pretty well. I'll try and get to that next week.

There are other things on my TODO list, like make the window that gets opened have the file name in its title...



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: klktrk on Aug 30, '07 11:29:57PM

Lovely! Just added the alias to my .bash_profile, and I have much command line joy.

---
The Apotek
http://theapotek.com
The Executioner's Summary:
http://www.last.fm/label/Broken+Hill+Music/playlists/6761?autostart



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: TGV on Aug 30, '07 11:31:44PM

The argument substitution problem goes away if you put this in your .bash_profile (or a similar line in your shell of preference's start up file):

alias drag=<path-to-app>/Drag.app/Contents/MacOS/Drag

That alllows it to receive all (i.e., more than one) argument with spaces (or semicolons...) in their file name.



[ Reply to This | # ]
single quotes?
Authored by: klktrk on Aug 30, '07 11:38:24PM
should probably be:
alias drag='/Drag.app/Contents/MacOS/Drag'

---
The Apotek
http://theapotek.com
The Executioner's Summary:
http://www.last.fm/label/Broken+Hill+Music/playlists/6761?autostart

[ Reply to This | # ]

Use a free program to simplify dragging from Terminal
Authored by: Fairly on Aug 31, '07 11:32:19PM

This is i386 only.



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: Fairly on Aug 31, '07 11:34:35PM

It's also Apple code. Five years old. Thanks for the contribution.



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: Lliwynd on Jan 29, '10 06:53:20PM

Yeah - this was based on Apple code, as freely acknowledged in the readme. The point wasn't to claim some great piece of software, it was to release weekend hack I thought others might find useful.

I'm honestly surprised there still isn't a standard tool for this.



[ Reply to This | # ]
Use a free program to simplify dragging from Terminal
Authored by: Lliwynd on Jan 29, '10 06:55:15PM

Just a quick update. I was going through some stuff and decided make this code more accessible in case someone wants to do something more with it. My code is now on Google Code here:

http://code.google.com/p/dragfile/



[ Reply to This | # ]