getPath and goTo apps. Quick and easy location management

One of the features I love about Windows (yes, I said it), is the address bar. You can select, copy paste and type away to your hearts content. Outside of Path Finder, this is not easy in the Mac OS. Maybe 10.5 will fix this oversight… Until then, I (with a more than a bit of help from the forums here) created a pair of apps to ease the task of telling someone where you are working. The idea is that you drop them as apps in your ‘/Library/Scripts/’ folder. Then you drop each on the toolbar of a Finder window, so they become a part of the Finder windows. In a production environment working with many large servers, this is a wonderful way to tell someone where to find a file or folder you have been working on. These two are the latest versions I have, and anyone is more than welcome to use and modify them as they see fit.

Thanks to all the wonderful scripters for the guidance and help. I am really enjoying this journey.

getPath gets the POSIX path of the front window you have open.


tell application "Finder"
	try
		set theSelection to the selection
	on error
		display dialog "You must have something selected and be in list view." buttons {"OK"} default button 1
		error number -128
	end try
	try
		set thePath to theSelection as alias as string
	on error
		display dialog "You must have something selected and be in list view." buttons {"OK"} default button 1
		error number -128
	end try
	set theChosenPath to button returned of (display dialog "Choose HFS (:) or POSIX (/) format?" buttons {"HFS", "POSIX"} default button 1)
	if theChosenPath = "POSIX" then
		set thePOSIXpath to POSIX path of thePath
		set thePOSIXpath to thePOSIXpath
		set the clipboard to thePOSIXpath
	else
		set thePath to thePath
		set the clipboard to thePath
	end if
end tell

goTo opens a new window with the POSIX path you want to go to.


display dialog "Where would you like to go?" default answer "/"
set userInput to text returned of result
try
	set userInput to POSIX file userInput as alias
on error
	display dialog "The path does not exist or there is an extra return at the end of the string." buttons {"Cancel"} default button 1
end try

tell application "Finder"
	activate
	set newWindow to make new Finder window
	set target of newWindow to userInput
end tell

Model: Dual 2.3GHz G5
AppleScript: 2.1.1/Script Debugger 4
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

never had this problem, yo. that path bar in windows explorer is cluttery as well as being confusing for lots of users, too.

command-option-g and the Path button in the finder toolbar work well for me. ditto column view. :slight_smile:

In case you did not know, command-clicking on the proxy icon or title in the title bar of a Finder window, provides nearly the same functionality as the Path button in Finder’s toolbar. Safari also does something similar for URLs.

The basic problem is that there is no easy way to get the working path as text. The points are, however, perfectly valid for the second script.

Here’s an altered script to do the copying to the clipboard for you.

tell application "Finder"
	if exists (front Finder window) then
		set thePath to POSIX path of (target of front Finder window as alias)
		display dialog thePath & return & return & "Copy front Finder window path to clipboard?" buttons {"Cancel", "Copy"} default button 2
		set the clipboard to thePath
	end if
end tell

Rather nicely done, QD. I’ve embroidered it slightly for my own use:

tell application "Finder"
	if exists (front Finder window) then
		if button returned of (display dialog "HFS or POSIX Path?" with title "Path Type" buttons {"Cancel", "HFS Path", "POSIX Path"} default button 3) is "HFS Path" then
			set thePath to (target of front Finder window as alias) as string
		else
			set thePath to POSIX path of (target of front Finder window as alias)
		end if
		display dialog thePath & return & return & "Copy front Finder window target to clipboard?" buttons {"Cancel", "Copy"} default button 2
		set the clipboard to thePath
	end if
end tell

I appreciate all of the wonderful advice on this one. My goal was a way to simplify the process for some users who are not hotkey-friendly. A few of them are midrange users, but most are more creative-types and this dual-purpose script is perfect for them. Now that I have been reminded of the ‘go to’ command I will probably use it, as it seems to be a bit quicker.

Levon

For anyone interested, here is the latest code I have our users running for these scripts. No changes were necessary from 10.8 to 10.9, but I did find an interesting post at c|net with other alternatives to this. http://reviews.cnet.com/8301-13727_7-57578129-263/how-to-copy-a-file-path-in-os-x/?refresh=1395259999241

getPath:


tell application "Finder"
	try
		set theSelection to the selection
	on error
		display dialog "You must have something selected and be in list view." buttons {"OK"} default button 1
		error number -128
	end try
	try
		set thePath to theSelection as alias as string
	on error
		display dialog "You must have something selected and be in list view." buttons {"OK"} default button 1
		error number -128
	end try
	set theChosenPath to button returned of (display dialog "Choose HFS (:) or POSIX (/) format?" buttons {"HFS", "POSIX"} default button 2)
	if theChosenPath = "POSIX" then
		set thePOSIXpath to POSIX path of thePath
		set thePOSIXpath to thePOSIXpath
		set the clipboard to thePOSIXpath
	else
		set thePath to thePath
		set the clipboard to thePath
	end if
end tell

goTo:


display dialog "Where would you like to go?" default answer "/"
set userInput to text returned of result
try
	set userInput to POSIX file userInput as alias
on error
	display dialog "Path doesn't exist" buttons {"Cancel"} default button 1
end try

tell application "Finder"
	activate
	set newWindow to make new Finder window
	set target of newWindow to userInput
end tell

This is a version considering multiple selections and “active” error handling without try blocks.
If the user doesn’t press the HFS or POSIX button, POSIX will be selected after 2 seconds.
The script is not restricted to list view.


activate application "SystemUIServer" -- works around a Finder selection bug
tell application "Finder"
	activate
	set theSelection to (get selection)
	if theSelection is {} then display dialog "You must have something selected" buttons {"Cancel"} default button 1
	set {button returned:pathMode, gave up:gaveUP} to display dialog "Choose HFS (:) or POSIX (/) format?" buttons {"Cancel", "HFS", "POSIX"} default button "POSIX" giving up after 2
end tell
set resultList to {}
repeat with oneFile in theSelection
	set hfsPath to oneFile as text
	if pathMode is "POSIX" or gaveUP then
		set end of resultList to POSIX path of hfsPath
	else
		set end of resultList to hfsPath
	end if
end repeat

set {TID, text item delimiters} to {text item delimiters, return}
set resultList to resultList as text
set text item delimiters to TID
set the clipboard to resultList

For goTo type ⇧⌘G in Finder

Hello.

I read about the goto shorcut above, I have missed the toolbar version of that. I think that a great leap forward for OS X apps is that a toolbar is mandatory for all regular windows, and that every toolbar must be able to execute an applet. :slight_smile:

You’ll have to open the property list after having saved the script below as an applet, and add an item “Application is Agent”, which you must check off. (LSUIElment = 1 in XML).

-- http://macscripter.net/viewtopic.php?pid=171787#p171787
(*
	Copyright © 2014 McUsr. All rights reserved.
*)
script goTo
	property onlyFromToolBar : false
	if not onlyFromToolBar then
		do shell script "open -a \"Finder\" "
		# In order to "disturbe" the window layout as little as possible.
		tell application "Finder"
			try
				set probe to id of its Finder window 1
				# we check if we have a window after all
			on error
				make new Finder window at desktop folder
				# if we didn't
			end try
			tell its Finder window 1
				tell application id "sevs" to tell application process "Finder" to keystroke "g" using {command down, shift down}
				# perform the keyboard shortcut
			end tell
		end tell
	else
		tell application id "sevs" to tell application process "Finder" to keystroke "g" using {command down, shift down}
		# perform only the keyboard shortcut
	end if
end script

on run
	run script goTo
end run

Edit

The real cool thing is that it remebers the last folder you went to. It is often so that, you bring up the goto folder dialog for a folder that is awkward to navigate to from Finder’s sidebar, and it is often probable that you want to revisit that folder again, so easy access is gold! :slight_smile:

Edit++
I made it configurable to work from only the Toolbar, or from Toolbar, Spotlight bar (and stand-alone), as it then brings up the front finder window, and creates one if there weren’t any.

This is a really useful script. I’ve tried doing my own custom edit, but struggling a little.

Using this script.

activate application "SystemUIServer" -- works around a Finder selection bug
tell application "Finder"
   activate
   set theSelection to (get selection)
   if theSelection is {} then display dialog "You must have something selected" buttons {"Cancel"} default button 1
   set {button returned:pathMode, gave up:gaveUP} to display dialog "Choose HFS (:) or POSIX (/) format?" buttons {"Cancel", "HFS", "POSIX"} default button "POSIX" giving up after 2
end tell
set resultList to {}
repeat with oneFile in theSelection
   set hfsPath to oneFile as text
   if pathMode is "POSIX" or gaveUP then
       set end of resultList to POSIX path of hfsPath
   else
       set end of resultList to hfsPath
   end if
end repeat

set {TID, text item delimiters} to {text item delimiters, return}
set resultList to resultList as text
set text item delimiters to TID
set the clipboard to resultList

Ideally I’d like it to miss the choose HFS (:slight_smile: or POSIX (/) format and auto copy a POSIX (/) format to the clipboard.

Once copied maybe message comes up to say ‘copied’ for a quick moment or you hit enter and the message removes itself.

Doing this would mean your hands never have to leave the keyboard.

Any pointers out there for editing it? All help and insight gratefully received.

here we go


activate application "SystemUIServer" -- works around a Finder selection bug
tell application "Finder"
	activate
	set theSelection to (get selection)
	if theSelection is {} then display dialog "You must have something selected" buttons {"Cancel"} default button 1
end tell
set resultList to {}
repeat with oneFile in theSelection
	set end of resultList to POSIX path of (oneFile as text)
end repeat

set {TID, text item delimiters} to {text item delimiters, return}
set resultList to resultList as text
set text item delimiters to TID
set the clipboard to resultList
tell application "Finder" to display dialog "Copied" buttons {"OK"} default button "OK" giving up after 1


Works a treat, thank you Stefan.