Easy way to copy paste file paths on Macs
I am working on some long procedures that will need to reference a lot of file paths of documents located locally and on various external drives and servers. In all cases we are using Apple Macs with either El Capitan or Sierra installed.
My problem - it is taking me a long time to manually type and double-check the file paths are correct.
Preferred solution - I would love it if there was just a way to copy and paste the file paths into these procedures.
I did try using the Get Info option, but couldn’t see a file path listed anywhere that I could copy and paste.
I also read this question: Get components (path and filename) of POSIX filepath and did have a go of using the script. This was promising, but it doesn’t quite do what I need.
Is there an easy way for me to just copy and paste file paths as I need them?
For example, if I have an image called “Serial Number.jpg” located on an external drive called Server1 the file path would be: /Volumes/Server1/Serial Number.jpg
.
I think this is an option that will do exactly what you want. Using your Serial Number.jpg example, do as follows:
- In the Finder browse to the image called “Serial Number.jpg” located on Server1
- Right-click on the file to display the context menu
- Now press and hold the option key down
- Select the Copy “Serial Number.jpg” as Pathname option
- Now go to the procedure you’re editing and paste the pathname you just copied
This should result in your document having the /Volumes/Server1/Serial Number.jpg
path pasted into it.
Just use the same steps for any file, regardless of whether it’s stored locally, on an external drive, or on a server. This also works to get the file path of folders.
Keyboard shortcut
Thanks to Mateusz Szlosek for pointing out you can also use a keyboard shortcut. Instead of Steps 2 to 4 above, once you've selected the file you can use the option+command+C shortcut to copy the file path. Then you can paste it as usual.
Windows etc files
There are custom built utilities to format shares for other operating systems and sub/afp as well:
- PathSnagger 2 works very well for this
You can just drag & drop a file [or a whole swathe of them] into a text area to get its [their] path[s] - not in all apps but in many, including Terminal [& incidentally in the question/answer space on Stack Exchange too.]
Some apps, like BBEdit, do not support this and take the contents of the file insteat of the full path. For these apps a Command 'drag & drop' works as well.
I would preferable just drag & drop a file into a text area to get its path (see Tetsujin's answer)
If this does not work for you, consider writing a very small program in Qt.
From http://qtsimplify.blogspot.de/2013/01/drag-and-drop-files-into-your.html :
Create a new "Qt Gui Application" in Qt Creator.
Edit the header file, mainwindow.h, by adding the following headers:
#include <QDropEvent>
#include <QUrl>
#include <QDebug>
Reimplement the protected functions, dropEvent() and dragEnterEvent() protected:
void dropEvent(QDropEvent *ev);
void dragEnterEvent(QDragEnterEvent *ev);
In the mainwindow.cpp, add these lines:
void MainWindow::dropEvent(QDropEvent *ev)
{
QList<QUrl> urls = ev->mimeData()->urls();
foreach(QUrl url, urls)
{
qDebug()<<url.toString();
}
}
void MainWindow::dragEnterEvent(QDragEnterEvent *ev)
{
ev->accept();
}
The dropEvent() function is where you recover the name of all the files you drop into your application.
And lastly, add this line into your mainwindow constructor:
setAcceptDrops(true);
Then, e.g. if you want to have the paths in the clipboard, you only need to copy them there using QClipboard
QClipboard *clipboard = QGuiApplication::clipboard();
clipboard->setText(url.toString());
For single file drops, and a list for all files.
I use the Services
Context Menu "Path to Clipboard" in Finder (although more often I use the Context Menu of QuollEyeTree).
I am not sure if this is a default service, or one I created using Automator (mine is dated 2012), but it is trivial to create such a service, it it does not already exist.
A command line solution that I use for getting paths quickly is to use realpath from GNU Coreutils. I have installled coreutils
via MacPorts, where the binary is called grealpath
. You can then call it on a file and it will give you the path. I like to mix it with pbcopy
to quickly get a path into my clipboard: $ grealpath histograms.root | pbcopy