GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Copy path to clipboard, escaped for Javascript, C++


#1

See Also: Commands to copy selected filenames to the clipboard

The command below puts the full path to the selected items into the clipboard, modifying the paths so that each \ is doubled.

For example:

C:\Program Files\GPSoftware becomes
C:\Program Files\GPSoftware

This is useful if you want to paste paths into some C++ code (e.g. when writing a test app), where the backslashes have to be doubled to prevent them being interpreted as escape characters.

The “=unc” part isn’t important and can be removed if you don’t want Opus to use the UNC path when dealing with mapped network drives.

I’ll quickly explain what the two regexp pairs do:

[ul][li]b\(.*)#
\1|\2[/b]

That converts all backslashes \ into pipes |. Pipes were chosen because they are not legal filename characters and it should be safe to assume the filepath does not contain any of them to start with.

Regular expressions themselves use \ as an escape character, which is why \ is used in the top part.

[/li]
[li]b|#
\1\\\2[/b]

That converts the pipes | into double backslashes \.

[|] is used in the top part because | on its own has special meaning to regular expressions. I could also have used | instead, but there are enough backslashes flying around already.

\\ is used in the bottom part to insert \ into the string.[/li][/ul]

The # on the end of each regexp tells Opus to repeat the find & replace until the string stops changing. (This is an Opus-ism, and not part of standard regular expressions.)

The conversion is done in two stages because if we ask Opus to repeatedly convert \ into \ then it will get stuck in a loop and keep doubling-up backslashes that it had already doubled. (It would not get caught in an inifinte loop; eventually Opus gives up, but you’ll have a lot of backslashes in the string by the time it does.)