1

This bug/issue has been bugging me for many months. I've sunk hours trying to solve it. Any help/direction is super welcome!


When trying to run the following function it always throws an error when using certain diacritics (ä, etc.)

My end goal: Allow any language's special characters to be passed into the CLI interface.

func runShortcut(inputShortcut: String) {
    let shortcutsCLI = Process()
    shortcutsCLI.standardInput = nil //TODO: DTS Fix. This allows us to run the Shortcut!!!
    
    shortcutsCLI.executableURL = URL(fileURLWithPath: "/usr/bin/shortcuts")
    shortcutsCLI.arguments = ["run", inputShortcut.decomposedStringWithCanonicalMapping] //decomposedStringWithCanonicalMapping / composed
    print("Full args: ", shortcutsCLI.arguments) //Prints: Optional(["run", "ä"])
    
    do {
        try shortcutsCLI.run()
        print("Should've ran the shortcut...")
    } catch {
        print("\(error)")
    }
}

runShortcut(inputShortcut: "ä")

I've tried forming the input "string" as Unicode let str = "\u{00c4}", formed from binary, etc. Doesn't seem to matter how I get to the character/string "ä", it refuses to work.

I've also tried str.precomposedStringWithCanonicalMapping & str.precomposedStringWithCanonicalMapping, both unsuccessful.

Everything works fine, running from the CLI, Python & Go. I strongly believe it has something to do with how the string is being passed into the Process().

With that being said, I'm unsure if it's an issue with the String that's being passed in, or if it's an underlying issue with the Process().


I have saved the inputShortcut variable to a local .txt file, then using this command cat "/PathToTxt/a.txt" | xargs shortcuts run, & that works fine, no matter what characters I throw at it. This makes me believe it's less of an issue with the underlying String.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
SENTINELITE
  • 11
  • 1
  • 2
  • What error do you get with the code you posted? – HangarRash Dec 10 '22 at 04:08
  • So you are trying to remove the diacritic marks? That's not what `decomposedStringWithCanonicalMapping` does. – Sweeper Dec 10 '22 at 04:08
  • [Possible duplicate](https://stackoverflow.com/q/29521951/5133585) – Sweeper Dec 10 '22 at 04:09
  • @HangarRash the error is: "Error: The requested shortcut was not found", which is the default error from the Shortcuts CLI. – SENTINELITE Dec 10 '22 at 09:08
  • @Sweeper I'm not trying to remove the diacritic marks. In the case of the example, passing a standard "a" \u{0041}. I want the "ä" \u{00c4}? passed into the function. I can verify that using the standard "a" with the CLI throws the std error. I know you can combine two or more signs to form the character with diacritics, but my combinations were either unsuccessful or formed wrongly. Sorry for the confusion. – SENTINELITE Dec 10 '22 at 09:14
  • 1
    The underlying issue seems to be described in the documentation for `Process arguments`: *"converts both path and the strings in arguments to appropriate C-style strings (using fileSystemRepresentation) before passing them to the task through argv[]."*. The string `"ä"` gets turned into `"aÃà"` which is why the shortcut isn't found. – HangarRash Dec 10 '22 at 15:29
  • 1
    See [How to work around NSTask calling -NSString fileSystemRepresentation for arguments](https://stackoverflow.com/questions/18459711/how-to-work-around-nstask-calling-nsstring-filesystemrepresentation-for-argum) for a possible solution (look at the non-Objective-C answer). – HangarRash Dec 10 '22 at 15:35
  • This seems to have pointed me in the right direction. I'm able to process most of the character set that I have, with the exception being spaces. It's seeing them as another argument per space, but that's just some formatting stuff I presume. Thanks for the help, @HangarRash! – SENTINELITE Dec 11 '22 at 03:05

0 Answers0