Get the folder path from the front most finder window

Hi folks,

Almost total newbie here, loving the software but need some guidance.

I’m trying to build a macro that will get the finder folder path and pass that to terminal.

Also another one. Trying to write to a file. Is there a way to create that file if it doesn’t exist?

Thanks

Hey Lyle,

Run from an Execute an AppleScript action (text script).

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2011/10/16 02:11
# dMod: 2014/10/15 10:56
# Appl: Terminal
# Task: CD to Front Finder Window's Directory.
# Libs: None
# Osax: None 
# Tags: @Applescript
------------------------------------------------------------

# Try is elided out due to FastScripts' better error dialog.

tell application "Finder" to set _dir to insertion location as alias
tell application "Terminal"
  tell selected tab of front window
    if its busy = false then
      do script "cd " & (quoted form of (POSIX path of _dir)) in it
    else
      do script "cd " & (quoted form of (POSIX path of _dir))
    end if
  end tell
end tell

------------------------------------------------------------

(In general it’s a good idea to break up different requests into different posts.)

Keyboard Maestro's Write to a File action DOES create a file if there isn't one and completely rewrites it if it does exist.

Note:

There is also an Append Text to a File action, and it too will create a file if one doesn't exist.

-Chris

1 Like

Hi,

That looks cool. Have to admit I don’t know AppleScript so its a bit complex but I’ll give it a try.

Thanks

Hey Lyle,

You don't need to know AppleScript to run it.  :smile:

It'll just work.

-Chris

1 Like

Hi,

Oh yes I understand that, I’m a php dev. I just like to know how things work :smile:

Hey Lyle,

Very good. That script is easy to understand.

If you have questions about AppleScript feel free to contact me off-list at: kmforum@thestoneforge.com

Traditional programmers often hate AppleScript, but you can get a lot of work done with it.

References For Learning & Using Applescript

Learning & Using AppleScript & JavaScript for Automation (JXA)

-Chris

1 Like

Wow, fantastic resource, thanks.

1 Like

Hey,

The snippet works a treat however, and sorry to trouble you, but the next step is failing.
I’ve had a similar thing in other attempts too.

After the ‘get the folder’ script I’m running a “Execute shell script” with the following.

touch %Variable%log%

This results in permission denied. I’d appreciate your guidance.

Cheers

Hey Lyle,

That's because you're trying to use a Keyboard Maestro text token in the shell, and the shell doesn't like it.

Here's how to use a Keyboard Maestro variable in the shell.

echo $KMVAR_YourVariableName

NOTE:

This won't work in Terminal.app – only from an Execute a Shell Script action action.

-Chris

Oh, by the way:

If your file path has any spaces in it you need to quote your variable:

touch "$KMVAR_YourVariableName"

-Chris

Hi,
Sorry for the delay.

I'm still having a few issues with this. Undoubtedly me but still.
So, my finder action is simply to open terminal (iTerm in my case) at the location of the foremost Finder screen.

Now I have iTerm opening and everything is fine however when I try to CD to the folder it appears that the Applescript is giving me a duff folder.

cd /var/folders/_n/4g1xqtdn1wd3l07xrp13trb80000gn/T/Keyboard-Maestro-Script-831DAE1D-69A9-4884-A49E-8CDFF0831CE2:151:156

Is that something I'm doing?

The whole thing for reference.

Hi Lyle

I think your AppleScript is the problem.

Attached is a macro that works on my system. You could possibly solve this with one AppleScript, but I am not a AppleScript expert.

Keyboard Maestro “cd to current directory in Finder in iTerm” Macro

cd to current directory in Finder in iTerm.kmmacros (4.9 KB)

Hey,

Well that worked a treat. The only issue I have is when I then try to touch a new file for example I get a permission denied message.

Any thoughts about how to get around that?

Do you want to touch a file through Keyboard Maestro?
Where is the file located? Do you have permission to do it manually in iTerm?

To be honest, I’m experimenting at this stage.
I’m a developer so I build kind of fake test websites all the time.

Ultimately I’d like to build two of three smaller sites but iTerm keeps stopping me.

I tried this at the desktop location and also an external drive.
Randomly the desktop works but the external drive doesn’t.
It seems that there is something permissions wise that wont allow Keyboard Maestro to run scripts. It looks like, on first inspection, that within my user folder all is well and the scripts do run.
So I guess its actually permissions rather thab Keyboard Maestro itself.

Thanks.

Hey Lyle,

You're not returning anything appropriate from the AppleScript into your log variable.

You can test AppleScript using the Script Editor.app.

If you're going to use iTerm then use it for everything.

In the current iTerm session:

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/04/18 02:37
# dMod: 2016/02/15 13:26
# Appl: iTerm
# Task: CD to the Directory of the Front Finder Window (Desktop if no windows are open).
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @iTerm, @Front, @Finder, @Directory
------------------------------------------------------------

tell application "Finder" to set dirOfFrontWindow to insertion location as alias
set dirOfFrontWindow to POSIX path of dirOfFrontWindow
set dirOfFrontWindow to quoted form of dirOfFrontWindow

set shCMD to "cd " & dirOfFrontWindow

tell application "iTerm"
  tell the first terminal
    tell the current session
      set name to "New Session"
      write text shCMD
    end tell
  end tell
end tell

------------------------------------------------------------

In a NEW iTerm Session:

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/04/18 02:37
# dMod: 2016/02/15 13:39
# Appl: iTerm
# Task: Create a new terminal session.
#     : Then CD to the Directory of the Front Finder Window (Desktop if no windows are open).
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @iTerm, @Front, @Finder, @Directory
------------------------------------------------------------

tell application "Finder" to set dirOfFrontWindow to insertion location as alias
set dirOfFrontWindow to POSIX path of dirOfFrontWindow
set dirOfFrontWindow to quoted form of dirOfFrontWindow

set shCMD to "cd " & dirOfFrontWindow

tell application "iTerm"
  tell (make new terminal)
    set newSession to (make new session at the end of sessions)
    tell newSession
      exec command "/bin/bash -l"
      write text shCMD
    end tell
  end tell
end tell

------------------------------------------------------------

-Chris

Thanks to @ccstone.

As I thought there was a way to do this only with AppleScript.