Copy to New TXT File?

I'm trying to create a macro that copies a SimpleNote note to the clipboard, splits the title from the body of the note, creates a new plain text TXT file with the body as the body, and then prompts me to save the file with the first paragraph of the note (the title) inserted as the filename. I'm not having much luck.

Below is what I have so far, adapting this Evernote macro (and a bunch of disabled alternate method attempts). Copy happens, TextEdit creates a new document, pastes in the body, but then stops; nothing is saved. If I set the save document line to "save document 1 in "/Users/pariah1/Desktop/TEST DOC.txt"" I'm given the error "the document "Untitled 4" could not be exported as "TEST DOC.txt". You don't have permission."

Help?

One approach:

Save clipboard body as firstLine.TXT.kmmacros (19.1 KB)

function run() {
    // writeFile :: FilePath -> String -> IO ()
    function writeFile(strPath, strText) {
        $.NSString.alloc.initWithUTF8String(strText)
            .writeToFileAtomicallyEncodingError(
                strPath, true,
                $.NSUTF8StringEncoding, null
            );
    }

    var a = Application.currentApplication(),
        sa = (a.includeStandardAdditions = true, a),
        strClip = sa.theClipboard();

    if (strClip) {
        var lines = strClip.split(/[\n\r]/),
            intLines = lines.length,
            strHead = intLines > 1 ? lines[0] : undefined,
            strTail = strHead ? lines.slice(1)
            .join('\n') : undefined;

        if (strTail) {
            var strPath = (
                sa.activate,
                sa.chooseFileName({
                    withPrompt: 'Save As',
                    defaultName: strHead + '.txt'
                })
            ).toString();

            return (
                writeFile(strPath, strTail),
                strPath
            );
        }
    }
}

For a KM-“native” solution you can replace your “Execute AppleScript” action with this:


[Test] Save Text Document from Clipboard with Title.kmmacros (3.8 KB)

(You have to adapt the path in the last action.)

Perfect! Thank you.

Thanks, @Tom, but I was looking for a process that let me choose where to save the TXT file rather than always saving it to the TMP folder.

Ah, sorry. I understood that the file(base)name was the only variable.

For the record: you can always add a simple path prompt like this:



(You can drop a folder onto the Path field.)

[Test] Save Text Document from Clipboard with Title 2.kmmacros (4.0 KB)

7 posts were merged into an existing topic: JXA: Reading/Using Data from the Clipboard

Because the discussion between @ComplexPoint and myself became very off-topic and involved, but a very good discussion, I have moved it to a new topic:

Hey Tom

Guess things have changed as this script no longer works. It prompts for a file name then turns the target folder into a file. Do you have ideas on how to update this?

Also, could it be tweaked to simply output a file for each line of text in the system clipboard without the prompt?

If you are referring to the script download from this post, I just tried it and it still seems to work fine:

  1. I copied a simple, short text to the clipboard
  2. Launched the macro
    • Prompt appears and title gets auto-filled (first line of the text on clipboard)
  3. I dragged the desired destination (parent) folder into the second field
  4. Hit OK
    • File gets written with the selected title into the selected parent folder

Here a variant that writes each line from the clipboard as text file:

Screen Shot 2021-05-17 at 14.54.34-pty-fs8

[Test] Save Each Line from Clipboard as Text File.kmmacros (2.7 KB)

Since we have no prompt here, you have to pre-set the desired parent folder in the green action (replacing my example path).

As with the original macro, there aren’t any error checks, e.g. for filename-incompatible characters. But usually these get converted to compatible replacement chars. If not, you’d have to separate title/content and add some filtering for the title.

2 Likes

Brilliant!

Checking my understanding: The forward slash between the two variables "%Variable%Local Parent Folder%/%Variable%Local Line%.txt" is saying write each variable parsed by the "For each" action to the variable Local Parent Folder which is a pathway that is set in the first action. Yes? Or is there a clearer way to understand this?

Yes, the Local Parent Folder variable (set in the green action) must contain the path of your desired parent folder.

Then, the complete path (which is the parent path + the last path component aka filename) is composed in the Write action, for each clipboard line. The slash separates path components, as usual. (If you prefer, you could also add a slash at the end of your parent path (green action) and remove the slash from the Write action.)

Then, slash means something like ‘and’ which requires something both before and after, yes?

Also, why would I prefer to move where the slash occurs or are you pointing that the write action doesn’t require it be located somewhere particularly?

Lastly, thank you for being supportive!

Hey Bern,

If you don't understand computer paths by now, you need to put in a little study time. Path is a very basic and essential concept for working with files and folders (directories).

https://www.google.com/search?q=computer+path+explanation&oq=computer+path+explanation&aqs=chrome..69i57j33i160.4757j0j1&sourceid=chrome&ie=UTF-8

1 Like

Hey Chris,

My understandings are an inch deep and mile wide. Coupled with uncertainty about how KM might be doing something unique to KM leaves me without confidence in my knowledge.

As I drop pretending to know, the gaps I've glossed over show up. This is an instance of firming up the base.

This probably looks strange as I do not see many people doing this kind of work publicly. Maybe it's time for some private lessons. It's all study time now. :wink:

I get that. The way I've dealt with it in general is to create test cases and test, until I understand what's happening.

-Chris

1 Like