question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add validation that Invoke-Item opens directory in Finder on Mac

See original GitHub issue

For PR https://github.com/PowerShell/PowerShell/pull/4262 validation was added to Windows and Linux. On Linux, we change the default application to a shell command to verify the output. On Mac, you would use lsregister to do something similar, however, MacOS doesn’t have a straightforward way to register and unregister at runtime (no api). Registration is done as part of app creation and startup.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveL-MSFTcommented, Jul 25, 2017

@mklement0 got around to finishing this. your pointer to AppleScript really helped!

1reaction
mklement0commented, Jul 18, 2017

You can use AppleScript as follows (via its osascript CLI):

$appleScriptWindowCount = 'tell application "Finder" to count of windows'

$before = [int] ($appleScriptWindowCount | osascript)

Invoke-Item $PSHome

$startTime = Get-Date
while (((Get-Date) - $startTime).TotalSeconds -lt 5 -and ([int] ($appleScriptWindowCount | osascript) -eq $before))
{
  Start-Sleep -Milliseconds 100
}

$after = [int] ($appleScriptWindowCount | osascript)

$before + 1  | Should Be $after

('tell application "Finder" to POSIX path of ((target of front window) as text)' | osascript) | Should Be "$PSHOME/"

'tell application "Finder" to close front window' | osascript

Note the need to pipe the AppleScript source code to osascript, because argument passing on Unix is still horribly broken (I’ve lost track of the debate):

osascript -e 'tell application "Finder" to count of windows'

SHOULD work, but currently doesn’t, because the embedded " are discarded.

Remember: On Unix, no funny quoting business (no rebuilding of the command line) is needed nor should it be attempted after PowerShell has parsed a command line; the resulting values should be passed as literals, as part of an argument array to the target utility.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change how folders open in the Finder on Mac
The folder opens in a new tab or window, depending on your Finder settings. In the Finder, choose Finder > Settings, then click...
Read more >
Open a folder in vscode through Finder in macOS?
Open the 'Shortcuts' app on your Mac · On the Left Pane, navigate to 'Quick Actions' · On the top bar, press the...
Read more >
How To Fix These 10 Mac Finder Annoyances - YouTube
https://macmost.com/e-2483 There are many small ways to customize the Mac Finder that may get rid of small annoyances for some users.
Read more >
10 Ways To Quickly Access Your Most Important Folders On a ...
... folder to open and save documents all the time, you can quickly access that folder a variety of ways. You can add...
Read more >
15 Tips to Better Organize and Find Files on Your Mac
These tricks all involve the Finder, your Mac's powerful file manager.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found