forked from wasdwasd0105/7zip-pyside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSevenZHelperMacOS.py
151 lines (113 loc) · 4.69 KB
/
SevenZHelperMacOS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import base64
import struct
import sys
import objc
from Cocoa import NSPasteboard, NSPasteboardItem
from Foundation import NSURL, NSString, NSAppleEventManager, NSApplication, NSObject, NSBundle
from AppKit import NSWorkspace
from PySide6.QtWidgets import QMessageBox
from objc._pycoder import NSData
def copy_files_to_clipboard(file_paths):
# Get the general pasteboard
pasteboard = NSPasteboard.generalPasteboard()
# Clear the current contents of the pasteboard
pasteboard.clearContents()
# Create a list to hold the pasteboard items
pasteboard_items = []
for file_path in file_paths:
# Create a new pasteboard item
pasteboard_item = NSPasteboardItem.alloc().init()
# Create NSURL from file path
file_url = NSURL.fileURLWithPath_(file_path)
# Set the URL of the pasteboard item
pasteboard_item.setString_forType_(str(file_url.absoluteString()), "public.file-url")
# Add the pasteboard item to the list
pasteboard_items.append(pasteboard_item)
# Write the pasteboard items to the pasteboard
pasteboard.writeObjects_(pasteboard_items)
def reveal_in_finder(file_path):
if sys.platform == "darwin":
url = NSURL.fileURLWithPath_(file_path)
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs_([url])
def create_bookmark(path):
url = NSURL.fileURLWithPath_(path)
result_tuple = url.bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error_(
1 << 11, # NSURLBookmarkCreationWithSecurityScope
None,
None,
None
)
bookmark_data = result_tuple[0]
if bookmark_data:
byte_array = bookmark_data.bytes().tobytes() # Convert the NSData to a Python bytes-like object
encoded_data = base64.b64encode(byte_array) # Encode to base64
return encoded_data.decode('utf-8') # Convert the base64 bytes to string and return
return None
def resolve_bookmark(encoded_data):
# Decode the base64 string to get the raw data
byte_array = base64.b64decode(encoded_data)
# Convert the Python bytes-like object to an NSData object
data_obj = NSData.dataWithBytes_length_(byte_array, len(byte_array))
# Placeholder for potential error
error = None
# Try to resolve the bookmark
resolved_url, is_stale, error = NSURL.URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
data_obj,
1 << 10, # NSURLBookmarkResolutionWithSecurityScope
None,
None,
error
)
# If there's an error or if the URL couldn't be resolved, return None
if error or not resolved_url:
return None
return resolved_url.path()
def start_accessing_resource(encoded_data):
# Decode the base64 string to get the raw data
byte_array = base64.b64decode(encoded_data)
# Convert the Python bytes-like object to an NSData object
data_obj = NSData.dataWithBytes_length_(byte_array, len(byte_array))
# Placeholder for potential error
error = None
# Try to resolve the bookmark to get the NSURL object
resolved_url, is_stale, error = NSURL.URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
data_obj,
1 << 10, # NSURLBookmarkResolutionWithSecurityScope
None,
None,
error
)
# If there's an error or if the URL couldn't be resolved, return False
if error or not resolved_url:
return False
# Start accessing the security-scoped resource
res = resolved_url.startAccessingSecurityScopedResource()
return res
def stop_accessing_resource(encoded_data):
# Decode the base64 string to get the raw data
byte_array = base64.b64decode(encoded_data)
# Convert the Python bytes-like object to an NSData object
data_obj = NSData.dataWithBytes_length_(byte_array, len(byte_array))
# Placeholder for potential error
error = None
# Try to resolve the bookmark to get the NSURL object
resolved_url, is_stale, error = NSURL.URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
data_obj,
1 << 10, # NSURLBookmarkResolutionWithSecurityScope
None,
None,
error
)
# If there's an error or if the URL couldn't be resolved, return False
if error or not resolved_url:
return False
# Start accessing the security-scoped resource
res = resolved_url.stopAccessingSecurityScopedResource()
return res
def get_app_version():
try:
bundle = NSBundle.mainBundle()
info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
return info.get("CFBundleShortVersionString", "GitHub Source")
except:
return f'Unknown version'