IKImageBrowserView for ASOC

Hi All!

I’d like to learn how to inplement an Image Browser using AppleScriptObjC. I’ve been trying to follow the Image Kit programming guide, but I’m having a difficult time translating the cocoa bindings and methods over to an applescript-cocoa app. I imagine this is something ASOC’ers have done before, but I haven’t found any examples or threads on this topic. Any help would be greatly appreciated!

Thanks!

Hi,

Here is a controller script to get you started. I have just pulled this out of an old ASOC project where I was experimenting. I think this is the project on my website here. THImage is the project you require.

http://www.theaford.co.uk/applescriptobjc.html

All the best

Terry



script THBrowserController
	property parent : class "NSObject"
	
	property NSNotFound : 2.147483647E+9
	property NSOKButton : 1
	property pTabView : missing value
	property pBrowserTab : 0
	property pViewerTab : 1
	property pViewerController : missing value
	property pImageBrowser : missing value
	property pAddFilesButton : missing value
	property pImageSizeSlider : missing value
	property pImageView : missing value
	property pImages : missing value
	property pImage : missing value
	property pImportedImages : missing value
	
	on init()
		continue init()
		set pImages to current application's class "NSMutableArray"'s arrayWithCapacity_(10)
		set pImportedImages to current application's class "NSMutableArray"'s arrayWithCapacity_(10)
		return me
	end init
	
	on awakeFromNib()
		pImageBrowser's setDelegate_(me)
		pImageBrowser's setDraggingDestinationDelegate_(me)
		pImageBrowser's setDataSource_(me)
		
	end awakeFromNib
	
	--BROWSER ROUTINES
	
	on zoomSliderDidChange_(sender)
		pImageBrowser's setZoomValue_(sender's floatValue())
		pImageBrowser's setNeedsDisplay_(true)
	end zoomSliderDidChange_
	
	--FILE ROUTINES
	
	on addFiles_(sender)
		set tPanel to current application's class "NSOpenPanel"'s openPanel()
		tPanel's setCanChooseDirectories_(true)
		tPanel's setCanChooseFiles_(true)
		tPanel's setAllowsMultipleSelection_(true)
		tPanel's setAllowedFileTypes_(current application's class "NSImage"'s imageUnfilteredTypes())
		if tPanel's runModal() = NSOKButton then
			set tDir to tPanel's filenames()'s objectAtIndex_(0)
			set tDefaultManager to current application's class "NSFileManager"'s defaultManager()
			set tAttribs to tDefaultManager's attributesOfItemAtPath_error_(tDir, missing value)
			set tFileType to tAttribs's objectForKey_("NSFileType") as string
			if tFileType = "NSFileTypeDirectory" then
				addFolderOfFiles_(tDir)
			else
				addImagesWithPaths_(tPanel's filenames())
			end if
		end if
		pImageBrowser's setZoomValue_(0.5)
		pImageSizeSlider's setFloatValue_(0.5)
	end addFiles_
	
	on addFolderOfFiles_(tDir)
		set tFileList to current application's class "NSFileManager"'s defaultManager's contentsOfDirectoryAtPath_error_(tDir, missing value)
		set tFilepaths to tDir's stringsByAppendingPaths_(tFileList)
		addImagesWithPaths_(tFilepaths)
	end addFolderOfFiles_
	
	on addImagesWithPaths_(tFilepaths)
		set tImageCount to tFilepaths's |count|()
		if tImageCount > 0 then
			repeat with n from 0 to tImageCount - 1
				my addAnImageWithPath_(tFilepaths's objectAtIndex_(n))
			end repeat
			my updateDatasource()
		end if
	end addImagesWithPaths_
	
	on addAnImageWithPath_(tPath)
		set tImageObject to current application's class "THImageObject"'s alloc's init()
		tImageObject's setPath_(tPath)
		pImportedImages's addObject_(tImageObject)
		
	end addAnImageWithPath_
	
	--DATA SOURCE ROUTINES
	
	on updateDatasource()
		pImages's addObjectsFromArray_(pImportedImages)
		pImportedImages's removeAllObjects()
		pImageBrowser's reloadData()
		pImageBrowser's setNeedsDisplay_(true)
	end updateDatasource
	
	on numberOfItemsInImageBrowser_(tView)
		set tc to pImages's |count|() as number
		return pImages's |count|()
	end numberOfItemsInImageBrowser_
	
	on imageBrowser_itemAtIndex_(tView, tIndex)
		return pImages's objectAtIndex_(tIndex)
	end imageBrowser_itemAtIndex_
	
	on imageBrowser_removeItemsAtIndexes_(tView, tIndexes)
		tView's removeItemsAtIndexes_(tIndexes)
	end imageBrowser_removeItemsAtIndexes_
	
	on imageBrowser_moveItemsAtIndexes_toIndex_(tView, tIndexes, tDestinationIndex)
		set tTemporaryArray to current application's class "NSMutableArray"'s alloc's init()
		set tIndex to indexes's lastIndex
		repeat while tIndex ≤ NSNotFound
			set tIndex to indexes's indexLessThanIndex_(tIndex)
			if tIndex < tDestinationIndex then
				set tDestinationIndex to tDestinationIndex - 1
			end if
			set tObj to pImages's objectAtIndex_(tIndex)
			tTemporaryArray's addObject_(tObj)
			pImages's removeObjectAtIndex_(tIndex)
		end repeat
		set tArrayCount to tTemporaryArray's |count|()
		repeat with tIndex from 0 to tArrayCount - 1
			pImages's insertObject_atIndex_(tTemporaryArray's objectAtIndex_(tIndex), tDestinationIndex)
		end repeat
		return true
	end imageBrowser_moveItemsAtIndexes_toIndex_
	
	on imageBrowser_cellWasDoubleClickedAtIndex_(aBrowser, tIndex)
		set tImageObject to pImages's objectAtIndex_(tIndex)
		set tImagePath to tImageObject's imageRepresentation()
		pImageView's setImageWithPath_(tImagePath)
		pTabView's selectTabViewItemAtIndex_(pViewerTab)
	end imageBrowser_cellWasDoubleClickedAtIndex_
	
	on imageBrowserSelectionDidChange_(aBrowser)
		
	end imageBrowserSelectionDidChange_
	
	--GENERAL ROUTINES
	
	on numberOfImages()
		set tNumOfImages to pImages's |count|()
		return tNumOfImages
	end numberOfImages
	
	on currentImage()
		set tIndexSet to pImageBrowser's selectionIndexes()
		return tIndexSet's firstIndex()
	end currentImage
	
	on setCurrentImage_(tCurrentImage)
		set tIndexSet to current application's class "NSIndexSet"'s indexSetWithIndex_(tCurrentImage)
		pImageBrowser's setSelectionIndexes_byExtendingSelection_(tIndexSet, false)
	end setCurrentImage_
	
	on imageURL_(tIndex)
		set tImageObject to pImages's objectAtIndex_(tIndex)
		set tImagePath to tImageObject's imageRepresentation()
		return current application's class "NSURL"'s fileURLWithPath_(tImagePath)
	end imageURL_
	
	on imagePath_(tIndex)
		set tImageObject to pImages's objectAtIndex_(tIndex)
		set tImagePath to tImageObject's imageRepresentation()
		return tImagePath
	end imagePath_
	
	on scrollBrowserToVisible_(tIndex)
		pImageBrowser's scrollIndexToVisible_(tIndex)
	end scrollBrowserToVisible_
	
	
end script

I have the same question, I’d love to inspect the link you posted tellboy because I don’t quite understand how to bind the properties in your script to my interface. Unfortunately though your link is no longer active, has it been reposted anywhere else?

Thanks

Dylan