Skip to content

Instantly share code, notes, and snippets.

@geberl
Created February 29, 2020 19:12
Show Gist options
  • Save geberl/526f594d0856e4a2e1d33d5ec4ef6960 to your computer and use it in GitHub Desktop.
Save geberl/526f594d0856e4a2e1d33d5ec4ef6960 to your computer and use it in GitHub Desktop.
Get the file type from NsDraggingInfo's draggingPasteboard
func checkFileType(sender: NSDraggingInfo) -> Bool {
let pasteboard = sender.draggingPasteboard()
// Iterate over accepted types of currently selected Workflow in its order of preference
// Return as soon as a present type of the dropped item matches an accepted type of the Workflow
for acceptedType in Workflows.activeAccepts {
if String(acceptedType) == "filename" {
if pasteboard.types?.contains("public.file-url") == true {
// File URLs from Finder (files, folders, drives)
// XCode (file)
// Mail (email)
log.debug("Returning: filename")
return true
}
} else if acceptedType == "url" {
if pasteboard.types?.contains("public.url-name") == true ||
pasteboard.types?.contains("public.url") == true {
// Safari (image)
// Safari (url icon from adress bar)
// Safari (link)
// Vox (playlist item)
// iTunes (album, song)
// Mail (email)
// Calendar (item)
log.debug("Returning: url")
return false
}
} else if acceptedType == "image" {
if pasteboard.types?.contains("public.image") == true ||
pasteboard.types?.contains("public.tiff") == true ||
pasteboard.types?.contains("public.fax") == true ||
pasteboard.types?.contains("public.jpeg") == true ||
pasteboard.types?.contains("public.jpeg-2000") == true ||
pasteboard.types?.contains("public.camera-raw-image") == true ||
pasteboard.types?.contains("public.png") == true ||
pasteboard.types?.contains("public.xbitmap-image") == true ||
pasteboard.types?.contains("com.apple.macpaint-image") == true ||
pasteboard.types?.contains("com.apple.pict") == true ||
pasteboard.types?.contains("com.apple.quicktime-image") == true ||
pasteboard.types?.contains("com.apple.icns") == true ||
pasteboard.types?.contains("com.adobe.photoshop-​image") == true ||
pasteboard.types?.contains("com.adobe.illustrator.ai-​image") == true ||
pasteboard.types?.contains("com.compuserve.gif") == true ||
pasteboard.types?.contains("com.microsoft.bmp") == true ||
pasteboard.types?.contains("com.microsoft.ico") == true ||
pasteboard.types?.contains("com.truevision.tga-image") == true ||
pasteboard.types?.contains("com.sgi.sgi-image") == true ||
pasteboard.types?.contains("com.ilm.openexr-image") == true ||
pasteboard.types?.contains("com.kodak.flashpix.image") == true {
// Safari (image) [tiff, nothing else ever seen]
log.debug("Returning: image")
return false
}
} else if acceptedType == "plaintext" {
if pasteboard.types?.contains("public.plain-text") == true ||
pasteboard.types?.contains("public.source-code") == true ||
pasteboard.types?.contains("public.xml") == true ||
pasteboard.types?.contains("public.utf8-plain-text") == true ||
pasteboard.types?.contains("public.utf16-plain-text") == true ||
pasteboard.types?.contains("public.utf16-external-plain-text") == true {
// Sublime Text
// XCode (code)
// Safari (image)
// Safari (link)
// Safari (text)
// iTerm (text)
// Mail (email)
// Mail (content)
// Calendar (item)
// MacVim, VS Code and Atom don't support dragging from them
log.debug("Returning: plaintext")
return false
}
} else if acceptedType == "richtext" {
if pasteboard.types?.contains("public.rtf") == true ||
pasteboard.types?.contains("public.html") == true {
// TextEdit
// XCode (code)
// Safari (text)
// Mail (content)
log.debug("Returning: richtext")
return false
}
}
}
log.debug("Workflow doesn't accept any types of the dropped item, just \(Workflows.activeAccepts).")
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment