Skip to content

Commit b69ea1f

Browse files
committed
Add open_application_by_bundle_identifier, open_application_by_file_path
1 parent da5e167 commit b69ea1f

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

example/main.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
22
#include <pqrs/osx/workspace.hpp>
3+
#include <unistd.h>
34

45
int main(void) {
56
std::cout << "Finder: "
@@ -14,5 +15,11 @@ int main(void) {
1415
std::cout << "NotFound: "
1516
<< pqrs::osx::workspace::find_application_url_by_bundle_identifier("org.pqrs.NotFound") << std::endl;
1617

18+
pqrs::osx::workspace::open_application_by_bundle_identifier("com.apple.Safari");
19+
pqrs::osx::workspace::open_application_by_file_path("/System/Applications/Utilities/Activity Monitor.app");
20+
21+
// Wait open_application_*
22+
sleep(1);
23+
1724
return 0;
1825
}

example/project.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ targets:
44
example:
55
type: tool
66
platform: macOS
7-
deploymentTarget: "11.0"
7+
deploymentTarget: "13.0"
88
sources:
99
- path: main.cpp
1010
compilerFlags:

include/pqrs/osx/workspace.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ namespace pqrs {
1313
namespace osx {
1414
namespace workspace {
1515

16+
inline void open_application_by_bundle_identifier(const std::string& bundle_identifier) {
17+
pqrs_osx_workspace_open_application_by_bundle_identifier(bundle_identifier.c_str());
18+
}
19+
20+
inline void open_application_by_file_path(const std::string& file_path) {
21+
pqrs_osx_workspace_open_application_by_file_path(file_path.c_str());
22+
}
23+
1624
inline std::string find_application_url_by_bundle_identifier(const std::string& bundle_identifier) {
1725
char buffer[512];
1826

include/pqrs/osx/workspace/impl/impl.h

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ extern "C" {
1010

1111
// Do not use these functions directly.
1212

13+
void pqrs_osx_workspace_open_application_by_bundle_identifier(const char* bundle_identifier);
14+
15+
void pqrs_osx_workspace_open_application_by_file_path(const char* file_path);
16+
1317
void pqrs_osx_workspace_find_application_url_by_bundle_identifier(const char* bundle_identifier,
1418
char* buffer,
1519
int buffer_size);

src/pqrs/osx/workspace/PQRSOSXWorkspaceImpl.swift

+27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@
44

55
import AppKit
66

7+
@_cdecl("pqrs_osx_workspace_open_application_by_bundle_identifier")
8+
func pqrs_osx_workspace_open_application_by_bundle_identifier(
9+
_ bundleIdentifierPtr: UnsafePointer<Int8>
10+
) {
11+
let bundleIdentifier = String(cString: bundleIdentifierPtr)
12+
13+
if let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleIdentifier) {
14+
NSWorkspace.shared.openApplication(
15+
at: url,
16+
configuration: NSWorkspace.OpenConfiguration(),
17+
completionHandler: nil
18+
)
19+
}
20+
}
21+
22+
@_cdecl("pqrs_osx_workspace_open_application_by_file_path")
23+
func pqrs_osx_workspace_open_application_by_file_path(_ filePathPtr: UnsafePointer<Int8>) {
24+
let filePath = String(cString: filePathPtr)
25+
26+
let url = URL(filePath: filePath, directoryHint: .notDirectory, relativeTo: nil)
27+
NSWorkspace.shared.openApplication(
28+
at: url,
29+
configuration: NSWorkspace.OpenConfiguration(),
30+
completionHandler: nil
31+
)
32+
}
33+
734
@_cdecl("pqrs_osx_workspace_find_application_url_by_bundle_identifier")
835
func pqrs_osx_workspace_find_application_url_by_bundle_identifier(
936
_ bundleIdentifierPtr: UnsafePointer<Int8>,

tests/src/project.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ targets:
44
test:
55
type: tool
66
platform: macOS
7-
deploymentTarget: "11.0"
7+
deploymentTarget: "13.0"
88
sources:
99
- path: test.cpp
1010
compilerFlags:

0 commit comments

Comments
 (0)