Jump to content
vitor

TemplatesManager — Save and reuse files, directory structures, and urls

Recommended Posts

If you regularly need to set the same files or directory structures somewhere, say a set of prebuilt scripts and template files for certain types of regular projects, this workflow is meant to make your life easier.


It can take files, directory structures, and even URLs, and set them up as templates that’ll be copied over to your frontmost Finder window (if you’re using Path Finder, it will be used instead). Files and directory structures will be copied, and urls will be downloaded when requested (so you always get the latest version).

 

To keep your templates synced between machines, use the `lists_dir` Workflow Environment Variable to pick a custom save location.


If you have a template that consists of a directory, you can place inside it an executable script with the name starting as _templatesmanagerscript. (the extension will be your pick), to be executed automatically after copying.


It has a lot of options and you’ll likely use most of them, so I’ll fire through them succinctly:

  • Add to TemplatesManager [File Action] — Add a file or directory to your local templates.
  • tml (TemplatesManagerList) [Script Filter] — Show a list of your local templates. Type to sort with your query. Press ↵ to copy the selected one to your frontmost Finder window.
    • tml (with ⌘) — If the selected template is a directory, instead of copying the directory itself, copy what’s inside it.
    • tml (with ⌥) — Delete your template.
  • tme (TemplatesManagerEdit) [Keyword] — Open the templates directory so you can add, remove, and edit them manually.
  • rtml (RemoteTemplatesManagerList) [Script Filter] — Show a list of your remote templates (download name and url). Type to sort with your query. Press ↵ on the selected one to download the file to your frontmost Finder window.
    • rtml (with ⌘) — Paste contents of template file, instead of downloading it.
    • rtml (with ⌥) — Remove the url from your remote templates list.
  • rtme (RemoteTemplatesManagerEdit) [Keyword] — Open the remote templates urls file in a text editor so you can add, remove, and edit them manually.
  • rtma (RemoteTemplatesManagerAdd) [Keyword] — takes the URL in your clipboard and adds it to your remote templates list.

 

Download | Source

Edited by vitor

Share this post


Link to post

Update.

 

As promised, you can now sort through your templates with tml and rtml (just start typing), and if you set a _templatesmanagerscript. (you pick the extension) executable script inside a template’s directory, it’ll be executed after copying.

Share this post


Link to post

Update.


Added option to paste remote template contents (**rtml**). Also made a number of other fixes and improvements under the hood.


To update, download the latest version (same URL) or wait a few days (15 or less) and it’ll prompt you to on next usage, since it uses OneUpdater.

Share this post


Link to post

Update.


Sorting is now done by Alfred (you shouldn’t notice any difference). Also made some general code improvements and updated dependencies (again, you shouldn’t notice a difference).


To update, download the latest version (same URL) or wait a few days (15 or less) and it’ll prompt you to on next usage, since it uses OneUpdater.

Share this post


Link to post

Could this workflow be used to manage email templates? I find myself using a lot of the same email replies and it'd be nice to be able to save templates to a text file and pull out the text/template I need paste into an email. 

Share this post


Link to post
1 hour ago, boddicker said:

Could this workflow be used to manage email templates? I find myself using a lot of the same email replies and it'd be nice to be able to save templates to a text file and pull out the text/template I need paste into an email. 

 

Why not use the Snippets feature for that? Seems more appropriate.

Share this post


Link to post

This is great! I just faced a task where I needed to create a large amount of folders/subfolders according to a standardized structure - solved in very short time and with no errors thanks to this handy workflow - thanks! A minor thing, it doesn't seem to work for creating folders/files when using Path Finder instead of Finder, would this be easy to fix?

Share this post


Link to post

Glad to know it worked well for you, @cands.

 

Yes, it might be easy to make it work for Path Finder, depending on their AppleScript support. Since I haven’t used Path Finder in years, I’ll need you to perform a test.

  1. Open Path Finder in any directory.
  2. Open the Script Editor on your Mac and run this code:

tell application "Path Finder" to activate

tell application "System Events" to tell application "Path Finder" to return (POSIX path of (folder of the front window as alias))

What is the result?

Share this post


Link to post

Thanks for looking into it. The result is:

error "Can’t make folder of window 1 of application \"Path Finder\" into type alias." number -1700 from folder of window 1 to alias

Share this post


Link to post
5 hours ago, deanishe said:

tell application "Path Finder" to return (POSIX path of (target of front finder window))

 

Using this tell statement the result is the full path to the currently selected folder in Path Finder.

Edited by cands

Share this post


Link to post

I've found this script to work better. If you don't have the application, the above scripts will not work as well. This script doesn't stop working if you don't have one of the applications. I've also have other file managers in the list. This is what  I use for my Alfred Browser workflow.

 

on pathFinderFile()
	set appname to "Path Finder"
	tell application appname
		set pathfinderselection to selection
		if pathfinderselection is missing value then
			display dialog "Nothing selected" buttons {"Oh, crud..."} default button 1
			return
		end if
		set pathfinderpaths to ""
		repeat with i from 1 to count of pathfinderselection
			set pfItem to item i of pathfinderselection
			-- Corrects for error when hidden files are showing
			try
				set currentpathfinderpath to (POSIX path of pfItem)
				set pathfinderpaths to pathfinderpaths & space ¬
					& quoted form of currentpathfinderpath
			end try
		end repeat
		return currentpathfinderpath
	end tell
end pathFinderFile

on finderFile()
	set appname to "Finder"
	tell application appname
		try
			set filep to selection as alias list
			set finderSelection to POSIX path of filep
		on error
			display dialog "Nothing selected" buttons ¬
				{"Curses, foiled again!..."} default button 1
			return
		end try
	end tell
	return finderSelection
end finderFile

on commanderOneFile()
	set appname to "Commander One"
	tell application appname to activate
	tell application "System Events"
		keystroke "c" using {command down, control down}
		delay 0.5
	end tell
	
	return the clipboard
end commanderOneFile

on fmanFile()
	set appname to "fman"
	tell application appname to activate
	tell application "System Events"
		key code 103
		delay 0.5
	end tell
	
	return the clipboard
end fmanFile


set fileName to "testing"
set frontmostProcessName to "name"
tell application "System Events"
	set frontmostProcess to first process where it is frontmost
	set frontmostProcessName to name of frontmostProcess as text
end tell
if frontmostProcessName is "Finder" then
	set fileName to finderFile()
else if frontmostProcessName is "Path Finder" then
	set fileName to pathFinderFile()
else if frontmostProcessName is "Commander One" then
	set fileName to commanderOneFile()
else if frontmostProcessName is "fman" then
	set fileName to fmanFile()
end if
return fileName

 

Share this post


Link to post
3 hours ago, raguay.customct said:

If you don't have the application, the above scripts will not work as well.

 

Ah, of course. For a moment I forgot AppleScript is a steaming pile of garbage, and yet we need it to do things we wouldn’t be able to do otherwise (reminds me of the “small portions” joke in Annie Hall). What’s ridiculous is AppleScript has an exists that should work exactly for these cases. Yet, if the application exists it returns true, but if it doesn’t it asks you to locate it! That’s both bonkers and useless. I have an open radar (well, it’s closed as a duplicate of another one still open) asking for exists to either return false in that case, or for them to remove it.


But your code is still too complex and does a lot of things I wouldn’t want to (show dialogs, emulate key presses). I’d rather just take the chance to convert it to JXA instead. It’ll keep the code lean (about the same length and same logic) and won’t suffer from the problem of needing Path Finder installed.


Anyone knows how to get the path of the current Path Finder directory with JXA? For the Finder I’m doing decodeURIComponent(Application("Finder").insertionLocation().url().slice(7)).

Edited by vitor

Share this post


Link to post

From this page it seems that for Path Finder decodeURIComponent will not be necessary and url().slice(7) can be replaced with posixPath(). Now it’s just a matter of knowing what insertionLocation() can be replaced with.

Share this post


Link to post
45 minutes ago, deanishe said:

Without.

 

Thank you, thought as much. The Finder JXA method is ending with (which is contrary to the AppleScript way), hence the question.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...