Jump to content

Relative + Absolute Path Finder Workflow

Recommended Posts

So I'm trying to build a workflow where you would initially set whether you wanted to use the absolute path or relative path.


It would then recognize the file you are currently in based off of where you're cursor is (shouldn't matter what IDE you're in), also recognize the project directory that is open.  


You would then be able to fuzzy search (or just regular search) the current project directory for a file you want to include in your current file (ie: a CSS file or something).  When you hit enter, it would then either paste in the relative path or absolute path depending on what your initially set up.


Any help oh how to start this?

Edited by AleXander
Link to comment

Okay, so I thought this through a little more and here's what I've gotten so far.


I need to be able to grab the file path of the current project that I have open.  I thought of a couple different ways to do this.
First of all, at least for me, I only have 2 `workspaces` I used to house all of my projects on my machine.  `htdocs` and `git repositories`.  I'm assuming most developers do the same.  With this in mind, I feel like developers could set their project directories initially which would give me the place to stop when pattern matching in the next step.
I could then use Applescript to grab the current file, match that absolute path with the `projects` in their `workspace` to determine the `current project's directory`.
I've found the following Applescript code from another workflow but being that I'm mostly `php` proficient, I am having trouble passing the output of the Applescript to php.
I know there is a way to just grab the current project directory via Applescript but I can't figure out how.
Could I get a wee bit of help on this please! :)
Also, here are the steps I've thought of to complete this workflow.  Any suggestions / advice on things I might have planned out wrong?
Step 1 (aka: pre-search - initialized by hot key)
  • Settings:
    • Choose whether you want absolute path, or relative path to be default when pasting path in [need help figuring this part out]
    • ie: relative 
  • get path of project directory [i feel like there's a better a better way to do this.  Any way to do it in bash?]
    • path to find: /users/username/path/to/workspace/current_project
    • ideas:
      • have the user initially set their "workspaces" where they usually put their projects (not preferred)
      • grab the path via Applescript for whatever IDE/Text Editor the user is currently in [how do I grab this?]
  • get path of current file
    • path to find: /users/username/path/to/workspace/current_project/views/elements/header.php
    • possible logic 
// filename: get_path.scpt
on getPath()
	-- get path to frontmost document
	set frontPath to (path to frontmost application as text)

		tell application (frontPath)
				set p to (path of document 1) -- preview uses this style
			on error
		set p to (do shell script "ruby -rcgi -e 'print CGI.unescape ARGV[0][7..-1]' " & quoted form of p)
		if p starts with "localhost" then
				set p to (file of document 1) -- keynote, pages uses this style
			end try
		end tell
		set p to POSIX path of p

	on error

		-- if all else fails try to get it through system events

		tell application "System Events"
			set theprocess to the first process whose frontmost is true
			set thewindow to the value of attribute "AXFocusedWindow" of theprocess
			set thefile to the value of attribute "AXDocument" of thewindow as string
		end tell
		--tell application "System Events" to tell (process 1 where frontmost is true)
		--	value of attribute "AXDocument" of window 1
		--end tell

		set p to result
			set p to (do shell script "ruby -rcgi -e 'print CGI.unescape ARGV[0][9..-1]' " & quoted form of p)
		end if

	end try

	return p
end getPath

set p to getPath() of pathscript

-- get parent directory
do shell script "dirname " & quoted form of p
return p                                          [What am I doing wrong here?]

// then grabbing the the output of p in the php file

// filename controller.php
include_once 'get_path.scpt';
include_once 'Workflows.php';
$w = new Workflows;

$path = exec('osascript get_path.scpt'); //               [Why do neither of these work? Output: "Parse error: parse error in Command line code on line 1"]
// $path = passthru("/usr/bin/osascript get_path.scpt"); 


Step 2 (aka: search)
  • set the search location to current project's directory
  • get path of queried file
    • ie: /users/username/path/to/workspace/current_project/assets/css/main.cs
  • set paths relative to current project (queried file & current file)
    • ie: remove the "/users/username/path/to/workspace/ 
  • where the current_project names match, stop removing pre-directories
    // aka left with:
    $current_file = '/current_project/views/elements/header.php';
    $queried_file = '/current_project/assets/css/main.css';
  • generate path to be pasted in
    • possible logic 
                   // find how many "../'s" are needed
                   // find the path moving out
                   $dotdot_slashes = count(explode('/', $current_file)) - 1; //don't move up passed current_project
                   $path_out = str_repeat('../', $dotdot_slashes);

                   // find the path moving mack in
                   $remove_root_from_queried_file = array_shift(explode('/', $queried_file));
                   $path_in = implode('/', $remove_root_from_queried_file);

                   // path to be pasted in
                   $final_path = $path_out . '/' . $path_in;
  • return $final_path

Any help would be greatly appreciated! :)

Edited by AleXander
Link to comment

Your controller.php isn't working because you're including an AppleScript. include is only for PHP code. You don't need to include the AppleScript, just run it and grab the output.

Which IDE/editor are you using? Unless it's a native Cocoa editor, you aren't going to have much joy getting the filepath with AppleScript. It might be a better idea to write a plugin for the editor that calls Alfred with the filepath rather than the other way around (Dash works this way, for example).

You can determine the project root easily enough by climbing up the FS tree till you find .git or htdocs. If you take the editor plugin route, it may be possible to ask the editor for the project root, too, and pass that to Alfred as well.

Here's a PHP function to get the relative path from one file to another.

Link to comment



Sorry, I have been real busy lately. Check out this test workflow:




I fixed the AppleScript (there were a few syntax mistakes) and it somewhat works. It is embedded in the workflow as an AppleScript and being called from PHP as a osascript program. You can compare that to what you had. You do not include an osascript into PHP, that is only for PHP. And, the output variable is not returned from exec, but should be the second parameter.


But, this still does not work for many editors. Sublime gives the first opened file of that session, not the current front most file. I have a hard time getting Sublime to work with AppleScript well. I think most other IDEs on the Mac this should work okay. It works fine for MacVim.


I would set it up similar to my project management workflow in the Advanced Alfred Workflows article (http://computers.tutsplus.com/tutorials/alfred-workflows-for-advanced-users--mac-60963). There, you set a current project directory and create commands that work from that point. That would simplify things some.



Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...