如何使用Swift在OS X上读取Finder图标(左侧源列表) [英] How to read Finder icons (left source list) on OS X using Swift
问题描述
我尝试读取有关在左侧源列表中的finder中显示的图标的信息。我已经尝试了 NSFileManager
与以下选项
-
NSURLEffectiveIconKey
图标读取与finder中的不同 -
NSURLCustomIconKey
- 返回nil -
NSURLThumbnailKey
- 返回nil -
NSThumbnail1024x1024SizeKey
- 返回nil
我设法使用NSFileManager读取所有挂载的设备,但我不知道如何读取与设备连接的图标?也许有人有任何想法或暗示。
我也尝试使用
var image:NSImage = NSWorkspace.sharedWorkspace()。iconForFile((url as!NSURL).path!)
但它返回与 NSURLEffectiveIconKey
相同的图片
谢谢!
此答案是对
更新2
请注意,虽然它已被弃用,仍然可以工作。之前的解决方案中的 [LSSharedFileListItemRef]
的转换现在被忽略,因此我们必须转换为 NSArray
项目后:
如果let items = itemsCF.takeRetainedValue()as? NSArray {
for item in items {
let icon = LSSharedFileListItemCopyIconRef(item as!LSSharedFileListItem)
let image = NSImage(iconRef:icon)
// use image ...
}
}
I try to read information about icons that are shown in finder on left source list. I tried already NSFileManager
with following options
NSURLEffectiveIconKey
icon read is not the same as in finderNSURLCustomIconKey
- returns nilNSURLThumbnailKey
- returns nilNSThumbnail1024x1024SizeKey
- returns nil
I managed to read all mounted devices using NSFileManager but I have no clue how to read icons connected with devices? Maybe someone has any idea or a hint.
I also tried to use
var image: NSImage = NSWorkspace.sharedWorkspace().iconForFile((url as! NSURL).path!)
but it returns the same image as NSURLEffectiveIconKey
Thanks!
This answer is a translation to Swift 1.2 of Ken Thomases's Objective-C answer.
All credits go to Ken Thomases, this is just a translation of his awesome answer.
let listBase = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListFavoriteVolumes.takeUnretainedValue(), NSMutableDictionary())
let list = listBase.takeRetainedValue() as LSSharedFileList
var seed:UInt32 = 0
let itemsCF = LSSharedFileListCopySnapshot(list, &seed)
if let items = itemsCF.takeRetainedValue() as? [LSSharedFileListItemRef] {
for item in items {
let icon = LSSharedFileListItemCopyIconRef(item)
let image = NSImage(iconRef: icon)
// use image ...
}
}
Explanations:
When translating Ken's answer from Objective-C to try and use it I encountered some difficulties, this is the reason why I made this answer.
First problem was with LSSharedFileListCreate
, the method signature in Swift didn't accept nil
as its first parameter. I had to find a constant representing a CFAllocator
: kCFAllocatorDefault
. And the third parameter didn't accept nil
either, so I put a dummy unused NSMutableDictionary to keep the compiler happy.
Also the "seed" parameter for LSSharedFileListCopySnapshot
didn't accept the usual var seed:Uint32?
for the inout, I had to give a default value to seed
.
For deciding when to use takeRetainedValue
or takeUnRetainedValue
when using these APIs I referred to this answer.
Last, I had to cast the returned array as a Swift array of LSSharedFileListItemRef
elements (it was initially inferred as a CFArray by the compiler).
Update
This has been deprecated in OS X El Capitan 10.11 (thanks @patmar)
Update 2
Note that while it's been deprecated it still works. The cast as [LSSharedFileListItemRef]
in the previous solution is now ignored so we have to cast as NSArray
instead then cast the item later:
if let items = itemsCF.takeRetainedValue() as? NSArray {
for item in items {
let icon = LSSharedFileListItemCopyIconRef(item as! LSSharedFileListItem)
let image = NSImage(iconRef: icon)
// use image ...
}
}
这篇关于如何使用Swift在OS X上读取Finder图标(左侧源列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!