Save TextEdit document and set Finder tag?

How can I save a TextEdit document and set the Finder tag (Green or Red) as part of the saving macro?

A keystroke Command-S brings up the dialog where a tag can be set and tabbing will move things around where I can select a tag and another tab will set it. So UI hacking will get there.

If scripting the save dialog box is an onerous task then I’ll stick with using the UI but if someone has already ventured there…

Hey Bern,

Why in particular are you messing with TextEdit? There are better, more scriptable options.

You can’t directly script a save dialog unless you own a copy of Default Folder, although you can use UI-Scripting to improve things a bit.

What steps of the save process are you automating?

-Chris

As long as System Preferences > Keyboard > Shortcuts has the All Controls option enabled, then a macro like this should work. I tested it under 10.11.6.

Save and Tag Green

That's not necessary.

Tabbing between the fields in the dialog works regardless of All Controls.

On my MacOS 10.12.6 system TextEdit comes up with the cursor already in the name field, so only 1 tab is required to move to the tags field.

And while typing <Return> will work it's a better/safer practice to use the Press a Button action to select the [Save] button.

And while we're brute-forcing the UI we may as well go whole hog:

TextEdit -- Set Name and Tags in Save Dialog v1.00.kmmacros (8.9 KB)

But it's possible to script this with a little work-around for Apple's negligence.

I'll post this method soon.

-Chris

Thanks for correcting my earlier comment about the need for simulating Tab twice. I don’t have 10.12 (or 10.13) on any system, but on 10.11.x, the need for Tab to be simulated once or twice will depend on if System Preferences > Keyboard > Shortcuts is set to Text Boxes And Lists Only (Tab once) or All Controls (Tab twice).

And on 10.11.x, pressing Return doesn’t simulate a button click in the method I showed earlier, it formally enters the desired Tag while keeping the dialog open for other user actions. Again, I don’t know if the behavior is different in 10.12 or 10.13.

True on MacOS 10.12.6.

Ah. True as well.

I should have turned on “All Controls” and tested.

Sorry.

-Chris

Hey @BernSh,

Okay, this is nearly instant.

The orange colored actions are USER-SETTINGS and should be fairly self-explanitory.

The “Save Location” action will accept either a full POSIX Path or a Tilde-POSIX Path.

The macro will error if there's already a file of the given name at the save location.

For anyone poking around the script – the part where an empty file is written is to work around an EVIL bug (BAD Apple!) that keeps a variety of applications from saving via AppleScript (since Sierra came out if I recall correctly).

-Chris


TextEdit -- Save Front Document and Tag via AppleScript v1.00.kmmacros (10 KB)

Hey Chris

Thanks for stepping in.

Overview: Since the Mac SE I've be wrangling my computers to get them to do what I need them do while also evolving what it is that I need to do, so it's an emergent process.

I'm currently focused on simplifying. As I collect and organize bits of data on a range of topics, I've found I often need to includes notes in the folders the bits get saved in. I've also settled (tho not written in stone) on using Finder tags to sort out do now from do later actions. I make TextEdit (TE) doc, type note, set tag green for now or red for later, navigate to save location, then hit return.

I'm all in Apple's ecosphere and save everything to iCloud Drive. TE is small, good enough for a simple note, maintained by and unlikely to be abandoned by Apple. It's at least readable from within iOS. I'd use the Notes app for read/write from both MacOS and iOS but you can't breakout individual notes to place and tag them where needed. I make notes in the Notes app in iOS and transfer to TE if needed.

I'm reconfiguring the UI around using KM palettes. Note the green and red checkboxes in this global palette:

The idea is to click one and get a new TE doc tagged green or red, then save where needed as a way to capture "to do" notes within proximity of the related material's folder. As the option to tag is baked into the save action, I thought it easier to go that route then figure a way to tag during the make new document side.

I do have Default Folder and recently put it aside as a step toward simplification. I've found that adding system extensions can be troublesome and complexifying, so avoid if possible. I'm looking to KM rather than other apps as the means towards this end. If using Default Folder here for this is a better balance then I'll reinstall it.

Here’s another, two-pronged approach that may or may not fit with what you’re trying to accomplish.

Part one is to use Keywords. In a TextEdit .rtf file (not a .txt file), using File > Properties there is a Keyword field. So let’s just assume that one keyword used is “Later.” Should you need to find files with that keyword, Spotlight can find them with a keyword:later search.

Should you adopt that method, you could then create a Stationary Pad file with any desired keywords. A Stationary Pad file is what is often called a template. You create a file with any desired attributes, save it, then in the Finder, in the File > Get Info window check the box for Stationary Pad. Whenever the original is opened, a duplicate is created and the original always remains unchanged.

All told, for all of the keywords you have, you could have a Stationary Pad file, and each of those could be launched by any number of means using Keyboard Maestro.

Besides all those potential benefits, the aspect of keywords that I appreciate is that they are tied into the file. If I move a file with the Later keyword saved within it, that keyword goes with the file if I move it to another device. Finder tags do not, and for my use cases that’s important, though that certainly may not be so for you.

Hey @ccstone,

I’m creating a new TE doc, typing a few characters then hitting the hot key and… nothing.

Added the step of hitting Command-S to bring up the save dialog then hitting Command-S and… nothing.

Your working with an AppleScript newbie who’s only read a few chapters into some beginners materials and is basically just beginning to learn terminology.

Is there something for me to edit within the macro’s AS setup? You said “orange colored actions are USER-SETTINGS and should be fairly self-explanitory”. Are these set at the time the macro is running or setup before hand?

Hi @NaOH,

Thank you for your thinking on this! Adding a keyword that can be sorted by is an interesting take. It opens up a number of sorting possibilities and wouldn’t be hard to make a macro for. I’m not clear it’d be simple to change a tag (i.e., a now tag to not now tag or vice versa). Also looking up the keywords on the iOS side might be tricky.

The baked in system tags are appealing in there integration between both Operating Systems.

I’m not clear it’d be simple to change a tag (i.e., a now tag to not now tag or vice versa).

You'd certainly have to change any keywords while the files are open.

Also looking up the keywords on the iOS side might be tricky.

As for organization, on the Mac I find saved Smart Folders do the trick well. I don't work with files much on iOS, but there may be a way to have Keywords and Tags, since I believe the iOS Files app supports Tags. I know in my setup if I needed to do that, I'd have Hazel look for .RTFs with specific keywords, then apply tags, but it may not be worthwhile to you to add additional software for that. I'll have to consider if there's a Keyboard Maestro-only way to easily achieve both, though others here may also have ideas.

Hey @BernSh

That should be impossible – unless the macro isn't activated.

As soon as you hit the hotkey the front untitled document name should change to “AAA -- Your Document Name.rtf”, and it should be saved in your ~/Documents directory with the designated tags.

Don't do stuff like that without asking questions first!  :wink:

Even so – you've been using Keyboard Maestro for quite a while now and should understand a little bit about paths and document names.

The macro when run as is (provided it's active) will save the front TextEdit document to:

~/Documents/AAA -- Your Document Name.rtf

That's YOUR home folder --» Documents --» AAA -- Your Document Name.rtf

I prefaced the example document with “AAA”, so it would sort to the top and be easy to find.

These variable names should not be difficult to understand:

(The “local” prefix simply means the variable is NOT saved after the macro executes.)

local_SaveDirectory  ==  The directory where the document is saved.
local_DocName        ==  The document name.
local_DocTags        ==  The tag names for the saved document.

Of course.

The current save-location and document name are HARD-CODED. If you want them to be different than the demonstration save-location/name I provided then you have to be responsible for changing them.

There are a number of ways to do that.

  • You could use a Prompt for User Input action to provide a document name, which would then be saved at the default location.

  • You could do the same thing but provide a complete path.

  • You could use the first line in the new document as the save-name.

  • You could do the above and provide a complete path.

  • You could have the document save into a default directory with an automatically generated date-stamped name.

You have many choices.

Oh, one caveat – at present the contents of the front document will be saved to the NEW document, even if the FRONT document has already been saved somewhere.

IF the front document has already been saved then it's name will NOT change.

This means you can do things like save the state of a document at a certain point and return to it if needed.

Of course it's easy enough to change the macro to throw an error if the front document is already saved.

-Chris

Any time a macro does “nothing”, you should use the Assistance window to figure out why.

Select Help ➤ Assistance to open the Assistance window, and then select “something expected is not happening” and follow the instructions. That will tell you if the macro is firing at all. Most commonly when “nothing” happens it is because the macro is not being triggered.

If the macro is being triggered, then you can focus on the actions.

Ok, Turtle has poked his head out, and legs are slowly coming out, and hey, look, he's walking, good things are happening! :turtle:

Yup, I'm following.

Ok, I need to learn how to do this. But first I need to back up a few steps.

I'd like to rewrite this to have it make a new document, set the tag of the new document, have the option to make up a new name for the document, then let me pick the directory to save it in.

I added an Activate A Specific Application to open TE then Type a Keystroke (command N) to get a new doc going. This works to create a hard coded titled doc to the desktop as I changed the local_SaveDirectory variable to Desktop.

The next step is beyond me.
I'm guessing to set the tag next the local_DocTags variable must be moved up to be the next action and I can simply change it to Green as that is what I want the first macro to do. I'll create a second macro to create a doc with a Red flag.

Then I will need to have the Document Name variable give me the option to create a name for the doc before moving to the next step of picking a directory. I'm guessing a Prompt for User Input action would be the next step using the same local_SaveDirectory name but the to: is going to have to be something like %variable%directory% that is then reflected in the AppleScript but I'm over my head.

I poked around and found this:

This seems in the right direction but my understanding had too many gaps to use this and this seems to be where I generally get stuck learning AppleScripts. Most stuff I find is either too basic/general and I can't translate it into a specific application or it's too specific and there are gaps between it and where I am that I don't know how to fill.

Turtle goes back into shell for a rest 41 AM

You must have this boilerplated as I've seen it so many times. Maybe you even have a way to scan new posts and pick up certain phases that trigger a macro that auto posts this :wink:

I'd be useful to create a practice to actually apply this.

Thank you for your time, attention, and patience.

1 Like

Yes, it is actually mostly boilerplate from my email support templates.

It would be ideal if Keyboard Maestro itself could prompt this, but it can't. And actually, yes, if I had a smart enough AI to figure out a post warranted this response, it would be fantastic if it could be automatic. But we're not there yet :wink:

Hey Bern,

You cannot set a tag prior to saving the document, unless you work with the save dialog UI.

See @DanThomas' macro (here) for the basics of doing that.

This method will be much slower than mine, but it's easier to understand.

If you want to use mine then the simplest thing to do is to add the save location/file-name and the tags as line 1 and line 2 of the document.

If you have favorite save-locations, you can create text-abbreviation macros for them.

So, you hit Cmd-N.
Type “tdir” (or whatever your abbreviation for the desired directory is).
Type the file name and <return>.
Type the tags (or use 1 or more abbreviation macros to do it for you).

You end up with an unsaved document that looks like this:

/Users/chris/test_directory/<file name>
tag1, tag2, tag3
<blank line>
Your text ...

You hit your Save macro, and the document instantly saves to the given destination with the given tags, and the first three lines are automatically deleted.

Of course this can be adjusted to account for no tags and/or a missed blank line between the header and your text.

You could even use a Prompt with List action to pop-up and provide you with any number of save locations for line one.

If you commonly save these documents to the same place you could have a create new document macro that automatically emplaced the default directory and any default tags for you.

TextEdit is NOT very scriptable, but this method makes saving fast and flexible.

On the other hand – IF you want to freely navigate the file system in a save dialog then this method is NOT for you.

Is this starting to make sense?

-Chris

Yes it is making sense and I do want to freely navigate the file system.

The primary aim here was to save the steps required to set a tag by having two graphically identifiable macros in a palette where pressing one would create a TE doc with a red tag and pressing the other would do the same only with a green tag: 31 AM.

As it is now I press this 35 AM which simply opens a new TE doc, puts in the date and time, inserts a couple of lines leaving the cursor ready for input three lines down into the new doc. I then type my note, type command-s bringing up the save dialog UI with the doc name field highlighted, type the doc's name, tab twice to the tag field, click on red or green tag from the drop down menu that appears as I have nine tags to pick from, tab out of the tag field to set the tag then click on the navigation drop down menu to get to where the note goes and hit return to complete the process.

I can't shorten the titling of the doc or navigating to the save location as they are always changing. Picking if it is going to be a red or green tag is fixed so I was looking to save those four actions of two tabs, click and tab out to set the tag. If as you say I can't do that up front then that "solves" this in the sense of completing my inquiry. The answer is I can't do it.

Chris, thank you for your time and attention. They are never owed and you give them generously and freely. The clarity and rigor you (and others) bring to this forum gives me the space to get clear for myself and is a real gift given. It's a privilege to have the opportunity to participate here.

1 Like

Back to this - I have a TextEdit doc with a tag set as a Stationary Pad. When I double click the file it opens as a new doc with the tag as expected, however when I use the" Open a File, Folder or Application" action, the folder containing the file is opened but a new document is not made. Suggestions?

--

Never-mind:
This Stationary Pad File Not Duplicated When Opened even while I can't edit the AS to fix and this Create a New File in the Finder from a Selection of Templates bit of @ccstone & @JMichaelTX magic has given me what I need. Thanks guys!