Drag and Drop

the x-master

Riesenboiken
Registriert
29.01.06
Beiträge
290
Moin,

ich durchsuche gerade die Examples im AppleScript Studio und treffe dabei auf Drag and Drop.
Ich finde das Beispiel sehr interessant und probiere deswegen es zu schaffen, dass wenn man eine Datei oder mehrere auf eine Stelle zieht er dann die entsprechenden datein öffnet...
Im Beispiel gilt das ja nur für Bilder.
Klappt aber aus irgendwelchen gründen nicht.
Also für Drag and Drop benutze ich NSImageView.
Ich möchte das ganze aber in AppleScript machen…Das ist doch auch möglich, oder?

Grüße,

X.
 

Daisy

Uelzener Rambour
Registriert
14.01.06
Beiträge
366
the x-master schrieb:
Klappt aber aus irgendwelchen gründen nicht.
Also für Drag and Drop benutze ich NSImageView.
Ich möchte das ganze aber in AppleScript machen…Das ist doch auch möglich, oder?

Grüße,

X.

Hallo X,

ja, klar kann man Drag&Drop auch mit AppleScript erstellen - auch bei NSImageViews. Um sagen zu können, warum es bei dir nicht funktioniert, müsste man wieder mal ein paar mehr Details kennen ...

Grüße,

Daisy
 

the x-master

Riesenboiken
Registriert
29.01.06
Beiträge
290
OK...

also zu dem Code...

Das hier ist der vorgefertigte Code von Drag and Drop von Apple...

Code:
on drop theObject drag info dragInfo
	-- Get a list of the data types on the pasteboard
	set dataTypes to types of pasteboard of dragInfo
	
	-- Currently, we are only interested if there are "files names" on the pasteboard
	if "file names" is in dataTypes then
		-- This is a mechanism to tell the pasteboard which type of data we want when we access the "contents" of the pasteboard.
		set preferred type of pasteboard of dragInfo to "file names"
		
		-- Get the list of files dropped on the object form the pasteboard
		set thePaths to contents of pasteboard of dragInfo
		
		-- Load the image at the location of the first item
		set theImage to load image (item 1 of thePaths)
		
		-- Set the image into the image view
		set image of theObject to theImage
		
		-- Make sure to delete the image we loaded otherwise it will never be removed from memory.
		delete theImage
		
		-- Set the preferred type back to the default
		set preferred type of pasteboard of dragInfo to ""
	end if
	
	return true
end drop

So...nun habe ich mir gedacht vielleicht könnte man aus dem Code folgendes machen:

Code:
on drop theObject drag info dragInfo
	
	set dataTypes to types of pasteboard of dragInfo
	open dataTypes
	return true
end drop

Oder...

Code:
on drop theObject drag info dragInfo
	
	set dataTypes to types of pasteboard of dragInfo
	open theObjeckt
	return true
end drop

Natürlich geht beides nicht......
In der Anlage sind Bilder. So würde ich es einstellen!


X.
 

Anhänge

  • Picture 12.png
    Picture 12.png
    43,3 KB · Aufrufe: 278
  • Picture 11.png
    Picture 11.png
    8,2 KB · Aufrufe: 238
  • Picture 10.png
    Picture 10.png
    151,3 KB · Aufrufe: 279

Daisy

Uelzener Rambour
Registriert
14.01.06
Beiträge
366
on drop theObject drag info dragInfo

set dataTypes to types of pasteboard of dragInfo
open theObjeckt
return true
end drop

Hallo X,

zunächst mal eine kleine Erläuterung:
dem on drop handler werden beim Aufruf zwei Argumente übergeben - theObject und dragInfo:
theObject enthält das Objekt (UI-Element), auf dem die Drag-Operation 'gelandet' ist. Das verwendest du z.B. zur Identifizierung des Drag-Ziels, wenn du mehrere dropbare Objekte in deinem Projekt hast.
und über dragInfo kommst du an die Informationen darüber, was gedroppt wurde.

Um also an eine Liste aller Files zu kommen, die auf deinem Objekt gedroppt wurden, kannst du 1:1 den ersten Teil des Codes von Apple verwenden.

Code:
on drop theObject drag info dragInfo
	-- Get a list of the data types on the pasteboard
	set dataTypes to types of pasteboard of dragInfo
	
	-- Currently, we are only interested if there are "files names" on the pasteboard
	if "file names" is in dataTypes then
		-- This is a mechanism to tell the pasteboard which type of data we want when we access the "contents" of the pasteboard.
		set preferred type of pasteboard of dragInfo to "file names"
		
		-- Get the list of files dropped on the object form the pasteboard
		set thePaths to contents of pasteboard of dragInfo
		
		log thePaths -- siehe unten
	else
		log "Nix" -- unter den gedroppten Sachen war kein File 

	end if

end on

Bau' das mal ein - wenn du jetzt Files auf das Objekt ziehst, enthält die Variable 'thePaths' eine Liste von Pfaden zu den gedroppten Files.

Ab dann stellt sich die Frage, was du mit der Information anfangen willst. Möchtest du die Files durch den Finder öffnen lassen? Dann müsstest du die Pfade, die jetzt noch im POSIX-Stil in der Liste abgelegt sind, in AppleScript-File-References umwandeln (POSIX file) und dem Finder sagen, dass er sie aufmachen soll: Tell app "Finder" to open ...

oder möchtest du die Files in deiner Applikation öffnen (wie im Beispiel von Apple)? Dann wird's etwas schwieriger: Du müstest die Links nach Typen sortieren (Text/Bild/Film ...) und für die Darstellung jeweils ein geeignetes Objekt zur Verfügung stellen (NSTextView, NSImageView etc.)

Verwende beim 'Basteln' einfach mal etwas mehr den 'log'-Befehl. Das ist immer sehr aufschlussreich, um zu sehehn, was mit deinen Variablen passiert (Ergebnisse siehst du im Fenster 'Run Log'

Grüße,

Daisy
 
Zuletzt bearbeitet:
  • Like
Reaktionen: 1 Person