Sublime Forum

Get path of file from last active sublime text window

#1

Hey guys,

I’ve been using DTerm for a while on the mac to pop up a basic terminal prompt with the path set to the file or dir currently in the current app. But since upgrading to the latest OSX, DTerm support isn’t that great so I’m looking at alternatives.

I have a hotkey set in iTerm2 to pop a term window up and now I just need to be able to set the path quickly in the terminal. I’ve got a script that sets the path to the last active Finder window, and I’d love to be able to do the same for ST. But I have no idea how to even start figuring out if there is a way through apple script or otherwise to ask Sublime for the path of the file which is in the last active tab in sublime.

Anyone got any ideas if I can query the path of the file in the active tab in ST from the command line?

Cheers,

Dave

0 Likes

#2

It’s possible to use the subl command line tool that ships with Sublime to get Sublime to execute a command (so long as Sublime is currently running). With that in mind, one possible solution would be a plugin such as the following:

import sublime
import sublime_plugin


class ActiveFileCommand(sublime_plugin.ApplicationCommand):
    def run(self):
        name = sublime.active_window().active_view().file_name() or ""
        sublime.set_clipboard(name)

This defines a command named active_file that puts the name of the currently active file in Sublime onto the clipboard, or an empty string if the active tab doesn’t have an assigned name yet.

This seems like the most expedient way to get the appropriate information out of Sublime to your script, assuming that AppleScript can access the clipboard. Otherwise it would need to do something else (like drop a file somewhere with the information in it or something).

With that in play, you would execute something like the following to execute the command:

subl --command "active_file" --background

This tells the running Sublime to execute the active_file command and not activate Sublime in the process. Once that executes the clipboard has the name of the active file for you to do with as you wish.

It won’t do what you expect if Sublime is not running currently, though; in that case Sublime will be started, be activated (even with --background) and not execute the command (because Sublime starts while plugins are still being loaded).

3 Likes

#3

Amazing! Thank you so much. You’ve saved me hours of scratching my head and possibly reading through the DTerm source code with a couple of lines of code that gets me what I need. Didn’t cross my mind to write a plugin and execute it from the command line.

I have a little bash script

$ cat sp.sh
#!/bin/bash

/usr/local/bin/e --command "activefile" --background
NEW_DIR=`pbpaste`
echo "Changing dir to $NEW_DIR"
cd "$NEW_DIR"

Took your plugin code and combined that. I have an alias set for that bash script - needs to be sourced not run ( source sp.sh ) and presto. Right into the directory of whatever I happen to be working on at the time.

Thank you again!

1 Like