Running a shell command in macOS

I got this code off another post a few years ago and am updating the program. The code reveals a file in the macOS finder.

var cmd as Text
var sh as new Shell

// Hightlight the file in Finder.

// Build a shell command
cmd = “open -R “””+ f.NativePath.ToText + “”“”

// and execute it
sh.Execute cmd

// Check the error code.
if sh.ExitCode <> 0 then f.parent.open

The code works fine but the analyser complains ToText if deprecated, but nothing else works. If I take it out the compile fails, expecting Text but got String. ToText works but the analyser doesn’t like it.
What should it be?

Jack

You also need to change cmd to String

var cmd as Text
var sh as new Shell

// Hightlight the file in Finder.

// Build a shell command
cmd = "open -R """+ f.NativePath.ToText + """"

// and execute it
sh.Execute cmd

// Check the error code.
if sh.ExitCode <> 0 then f.parent.open
2 Likes

It looks like the quotes are curly ones, maybe the copy/paste changed them?

Edit: the original code has curly

Fixed - thanks.

The original has curly quotes because they were pasted into a quote field, rather than a code option. Always use the </> button when pasting code, it prevents this sort of problem and makes everything more readable.

var cmd as String // Don't use Text here.
var sh as new Shell

// Hightlight the file in Finder.

// Build a shell command
cmd = "open -R """ + f.NativePath + """" // Then you don't need ToText here.

// and execute it
sh.Execute cmd

// Check the error code.
if sh.ExitCode <> 0 then f.parent.open

Or my preferred method, three backticks on a line before and after the code block:

```
Code
```

1 Like

Yes, the is what the </> button does. You can press it and then paste your code, it goes into the right place. It also allows you to work on an iPhone (which doesn’t seem to offer the back apostrophe on the keyboard.

1 Like

Maybe you didn’t see the other post where someone gave us the code to reveal item in Finder using Declare:

Declare Function objc_getClass Lib "libobjc.dylib" ( name as CString ) as ptr
Declare Function sharedWorkspace Lib "AppKit" selector "sharedWorkspace" ( obj As ptr ) as ptr
Declare Function selectFile Lib "AppKit" selector "selectFile:inFileViewerRootedAtPath:" ( obj as ptr, fPath as CFStringRef, rootFullPath as CFStringRef ) as Boolean
Dim workspace as ptr = sharedWorkspace( objc_getClass( "NSWorkspace" ) )
' assert ( workspace <> nil, CurrentMethodName + " is Nil")
Call selectFile( workspace, CetElt.NativePath, "")

CetElt is the folderitem you want to reveal.

Excellent that works and the analyser doesn’t complain.

Thanks.