Родительский класс NSImageView равен NSControl , поэтому, как и у любого другого элемента управления, у него есть цель / действие, которое вы можете указать, например:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property mainWindow : missing value
on run -- Script Editor example
my performSelectorOnMainThread:"doWindowStuff" withObject:(missing value) waitUntilDone:true
end run
on doWindowStuff() -- window stuff needs to be done on the main thread
try
set my mainWindow to makeWindow out of {{0, 0}, {400, 200}} given title:"Drag an image:"
set imageView to makeImageView out of {{100, 50}, {200, 100}}
mainWindow's contentView's addSubview:imageView
mainWindow's makeKeyAndOrderFront:me
on error errmess
display alert "Error" message errmess
end try
end doWindowStuff
to makeWindow out of frame given title:title : "" -- make and return a window
tell (current application's NSWindow's alloc()'s initWithContentRect:frame styleMask:7 backing:2 defer:yes) -- styleMask of 7 includes title, close and minimize buttons
if first item of frame is {0, 0} then tell it to |center|()
its setAutorecalculatesKeyViewLoop:true
its setTitle:title
return it
end tell
end makeWindow
to makeImageView out of frame -- make and return an image view
tell (current application's NSImageView's alloc()'s initWithFrame:frame)
its setTarget:me
its setAction:"imageDragged:"
its setEditable:true
its setImageScaling:(current application's NSImageScaleProportionallyUpOrDown)
return it
end tell
end makeImageView
on imageDragged:sender
activate me
display dialog "An image was dragged into the image view."
end imageDragged:
Чтобы сделать что-то вроде получения пути к файлу для отброшенного изображения, NSImageView необходимо будет разделить на подклассы. Для проекта XCode добавьте новый .applescript
файл для подкласса и установите его в качестве класса вашего imageView в Identity Inspector в IB:
script MyImageView -- NSImageView subclass
property parent : class "NSImageView"
property imagePath : missing value
on draggingEntered:sender
# additional stuff as desired
return current application's NSDragOperationCopy
end draggingEntered:
on performDragOperation:sender
set pasteboard to sender's draggingPasteboard()
set theOptions to {NSPasteboardURLReadingFileURLsOnlyKey:1}
set imageURLs to pasteboard's readObjectsForClasses:{current application's |NSURL|} options:theOptions
set draggedURL to first item of (imageURLs as list)
set my imagePath to draggedURL as text
return true -- data accepted
end performDragOperation:
end script
Вы также должны зарегистрировать приемлемые типы перетаскивания. для вашего imageView где-то в настройках AppDelegate:
imageView's registerForDraggedTypes:{"public.image"}
Путь к файлу будет доступен в свойстве imageView imagePath
, когда вы перетаскиваете что-либо в представление. См. Apple Перетаскивание тем программирования для получения дополнительной информации.