Skip to content

Commit 5ddecf1

Browse files
committed
adds .webloc support
Adds .webloc support for dropping a web url from browser into a .stapled file. Launching works as well without mods.
1 parent 1451db6 commit 5ddecf1

File tree

1 file changed

+61
-24
lines changed

1 file changed

+61
-24
lines changed

Stapler/StaplerApp.swift

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class StaplerViewModel: ObservableObject {
218218
return false
219219
}
220220

221-
func handleError(_ error: Error) {
221+
func handleError(_ error: Error) {
222222
DispatchQueue.main.async {
223223
self.errorMessage = error.localizedDescription
224224
}
@@ -300,29 +300,66 @@ struct ContentView: View {
300300
}
301301
.frame(minWidth: 300, minHeight: 200)
302302
.focused($isViewFocused)
303-
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
304-
let wasEmpty = viewModel.document.aliases.isEmpty
305-
for provider in providers {
306-
_ = provider.loadObject(ofClass: URL.self) { url, error in
307-
if let error = error {
308-
viewModel.handleError(error)
309-
} else if let url = url {
310-
DispatchQueue.main.async {
311-
do {
312-
let newAlias = try AliasItem(url: url)
313-
viewModel.addAlias(newAlias)
314-
if wasEmpty {
315-
updateDocument()
316-
} else {
317-
document.aliases = viewModel.document.aliases
318-
}
319-
} catch {
320-
viewModel.handleError(error)
321-
}
322-
}
323-
}
324-
}
325-
}
303+
.onDrop(of: [.url, .urlBookmarkData], isTargeted: nil) { providers in
304+
let wasEmpty = viewModel.document.aliases.isEmpty
305+
for provider in providers {
306+
_ = provider.loadObject(ofClass: URL.self) { url, error in
307+
if let error = error {
308+
viewModel.handleError(error)
309+
} else if let url = url {
310+
DispatchQueue.main.async {
311+
do {
312+
let newAlias = try AliasItem(url: url)
313+
viewModel.addAlias(newAlias)
314+
if wasEmpty {
315+
updateDocument()
316+
} else {
317+
document.aliases = viewModel.document.aliases
318+
}
319+
} catch {
320+
viewModel.handleError(error)
321+
}
322+
}
323+
}
324+
}
325+
}
326+
// If there are no items conforming to the specified types, check for file promise receivers in the pasteboard
327+
let pasteboard = NSPasteboard(name: .drag)
328+
guard let filePromises = pasteboard.readObjects(forClasses: [NSFilePromiseReceiver.self], options: nil),
329+
let receiver = filePromises.first as? NSFilePromiseReceiver else {
330+
return false
331+
}
332+
333+
// Process the first file promise receiver
334+
let queue = OperationQueue()
335+
receiver.receivePromisedFiles(atDestination: FileManager.default.temporaryDirectory, //URL.temporaryDirectory,
336+
operationQueue: queue) { (url, error) in
337+
if let error = error {
338+
print("Error loading dropped item from pasteboard: \(error.localizedDescription)")
339+
} else {
340+
DispatchQueue.main.async {
341+
// url is a .webloc
342+
let data = try? Data.init(contentsOf: url) as Data
343+
// turn webloc contents (xml) into plist dict
344+
let dict = try! PropertyListSerialization.propertyList(from:data ?? Data(), options: [], format: nil) as! [String:Any]
345+
let urlString = dict["URL"] as! String
346+
let newUrl = URL(string: urlString)
347+
do {
348+
let newAlias = try AliasItem(url: url)
349+
viewModel.addAlias(newAlias)
350+
if wasEmpty {
351+
updateDocument()
352+
} else {
353+
document.aliases = viewModel.document.aliases
354+
}
355+
} catch {
356+
viewModel.handleError(error)
357+
}
358+
359+
}
360+
}
361+
}
362+
326363
return true
327364
}
328365
.alert(isPresented: $showingErrorAlert) {

0 commit comments

Comments
 (0)