Difference between revisions of "macOS Open Sesame"

From Free Pascal wiki
Jump to navigationJump to search
(New macOS content :-)
 
m (→‎Opening Preference Panes: Remove dead link)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{Platform only|macOS}}
 
{{Platform only|macOS}}
  
The macOS '''/usr/bin/open''' command is a veritable Aladdin's cave of utility. The command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is specified, the default application as determined via LaunchServices is used to open the specified file. If the file is in the form of a URL, the file will be opened as a URL. See [[Executing External Programs]] to run the command from within your application.
+
== Overview ==
 +
 
 +
The macOS <tt>/usr/bin/open</tt> command line utility is a veritable Aladdin's cave of utility. The command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is specified, the default application as determined via LaunchServices is used to open the specified file. If the file is in the form of a URL, the file will be opened as a URL. See [[Executing External Programs]] to run the command from within your application.
  
 
== Opening files ==
 
== Opening files ==
Line 21: Line 23:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
   open -e test.c
 
   open -e test.c
 +
</syntaxhighlight>
 +
 +
== Opening directories ==
 +
 +
Want to open Finder at a particular directory, say the user's Documents directory?
 +
 +
<syntaxhighlight lang="bash">
 +
  open ~/Documents
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 43: Line 53:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
   open -n -a TextEdit
 
   open -n -a TextEdit
 +
</syntaxhighlight>
 +
 +
Want the <tt>open</tt> command to wait until the application opened has exited?
 +
<syntaxhighlight lang="bash">
 +
  open -W -n -a TextEdit
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 59: Line 74:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Note that this scheme was new in Yosemite (macOS 10.10) and that Apple subsequently restricted use of the URL scheme in El Capitan (macOS 10.11).  
+
Note that this scheme was new in Yosemite (macOS 10.10) and that Apple subsequently restricted use of the URL scheme in El Capitan (macOS 10.11).
 +
 
 +
== More options ==
 +
 
 +
For the full gamut of open command options, open a Terminal and call up the Unix man page:
 +
 
 +
<syntaxhighlight lang="bash">
 +
man open
 +
</syntaxhighlight>
  
See: [https://macosxautomation.com/system-prefs-links.html Apple: System Prefs Links]
+
== See also (cross-platform) ==
  
 +
* [[opendocument|OpenDocument]]
 +
* [[OpenURL]]
  
 
[[Category:macOS]]
 
[[Category:macOS]]
 
[[Category:Lazarus]]
 
[[Category:Lazarus]]

Latest revision as of 15:11, 31 March 2023

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

Overview

The macOS /usr/bin/open command line utility is a veritable Aladdin's cave of utility. The command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is specified, the default application as determined via LaunchServices is used to open the specified file. If the file is in the form of a URL, the file will be opened as a URL. See Executing External Programs to run the command from within your application.

Opening files

Want to open a test.c file?

  open test.c

Want to open your test.c file with TextEdit and not with Xcode?

  open -a TextEdit test.c

or

  open -e test.c

Opening directories

Want to open Finder at a particular directory, say the user's Documents directory?

  open ~/Documents

Opening URLS

Open our favourite website:

  open https://wiki.freepascal.org

Safari not your favourite? Use FireFox maybe:

  open -a FireFox  https://wiki.freepascal.org

Opening applications

Want to open another copy of a running application? Normally macOS would simply bring the already-running application to the foreground, but:

  open -n -a TextEdit

Want the open command to wait until the application opened has exited?

  open -W -n -a TextEdit

Opening Preference Panes

Want to open the Security Preferences Pane?

  open "x-apple.systempreferences:com.apple.preference.security"

How about the Firewall tab of the Security Preferences pane?

  open "x-apple.systempreferences:com.apple.preference.security?Firewall"

Note that this scheme was new in Yosemite (macOS 10.10) and that Apple subsequently restricted use of the URL scheme in El Capitan (macOS 10.11).

More options

For the full gamut of open command options, open a Terminal and call up the Unix man page:

 man open

See also (cross-platform)