Alle Bilder aus einem Ordner abfragen (Applescript)

D

djmaq

Aktives Mitglied
Thread Starter
Dabei seit
27.05.2008
Beiträge
276
Reaktionspunkte
0
Hallo.

Nach langer Zeit möchte ich nun auch mal wieder eine Frage stellen.

Ich habe in einer Skript Sammlung ein Skript gefunden das mir alle Bilder auf eine anzugebende Größe verkleinert.

Quellcode:
Code:
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "JPEG", "JPEG2", "jpg", "public.jpeg", ¬
	"PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}

--Get the artwork file
set theFiles to choose file with prompt "Choose art file(s)" of type openTypes with multiple selections allowed without invisibles
runConversion(theFiles)

on open someFiles
	runConversion(someFiles)
end open

on runConversion(theItems)
	set saveFolder to choose folder with prompt "Save resized pictures where?" without multiple selections allowed and invisibles
	display dialog "Max height?" default answer "600"
	set newHeight to (text returned of the result) as integer
	tell application "Image Events"
		launch
		set newWidth to 0
		if (count items of theItems) is greater than 0 then
			repeat with anItem in theItems
				set imageFile to (open anItem)
				set theSize to dimensions of imageFile
				set width to item 1 of theSize
				set height to item 2 of theSize
				set ratio to (width / height)
				set newWidth to (ratio * newHeight) as integer
				if newHeight > newWidth then
					scale imageFile to size newHeight
				else
					scale imageFile to size newWidth
				end if
				save imageFile as JPEG in saveFolder
				close imageFile
			end repeat
		else
			display dialog "Nothing to convert."
		end if
	end tell
end runConversion


Jetzt habe ich verändert das ich nicht die Höhe angeben kann, sonder die Breite. Das ist kein Hexenwerk und funktioniert auch.

Nun möchte ich aber eigentlich nicht einen Stapel Bilder auswählen, sondern einen Ordner in dem Bilder drin sind die dann entsprechend abgefragt werden. Dafür habe ich den oberen Teil vom Code angepasst.

Code:
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "JPEG", "JPEG2", "jpg", "public.jpeg", ¬
	"PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}

--Get the artwork file
set theFolder to choose folder with prompt "Ordner wählen"
tell application "Finder"
	set theFiles to every file of theFolder
	
end tell


runConversion(theFiles)



on open someFiles
	runConversion(someFiles)
end open

on runConversion(theItems)
	set saveFolder to choose folder with prompt "Save resized pictures where?" without multiple selections allowed and invisibles
	display dialog "Max height?" default answer "600"
	set newWidth to (text returned of the result) as integer
	tell application "Image Events"
		launch
		set newHeight to 0
		if (count items of theItems) is greater than 0 then
			repeat with anItem in theItems
				set imageFile to (open anItem)
				set theSize to dimensions of imageFile
				set width to item 1 of theSize
				set height to item 2 of theSize
				set ratio to (height / width)
				set newHeight to (ratio * newWidth) as integer
				if newHeight > newWidth then
					scale imageFile to size newHeight
				else
					scale imageFile to size newWidth
				end if
				save imageFile as JPEG in saveFolder
				close imageFile
			end repeat
		else
			display dialog "Nothing to convert."
		end if
	end tell
end runConversion

Das abfragen in dem Ordner an sich scheint nach Appleskript Editor auch zu funktionieren. Mir wird im Ergebnissbereich am unteren Fensterrand angezeigt das das die Liste mit den Dateien generiert wurde.
An der Stelle
Code:
set theSize to dimensions of imageFile
bekomme ich die Fehlermeldung das die Variable ImageFile nicht vergeben wäre. Ich vermute das es damit zu tun hat das die Dateitypen nicht über openTypes bekannt gegeben werden. Allerdings gelingt mir die Einbindung nicht. Egal wo ich es hinschreibe oder wie ich es probiere, jedes mal werde ich mit einer Fehlermeldung gestraft und das Skript bricht ab.

Wer kann helfen und mir sagen wie ich es anstellen muss das der Ordnerinhalt abgefragt und die Bilder das entsprechend umgewandelt werden?

vg
djmaq
 
Kann hier wirklich keiner helfen?
 
So sollte es gehen:

Code:
[B]property [/B][COLOR=#4F8F00][FONT=Verdana]openTypes[/FONT][/COLOR][FONT=Verdana] : {"JPEG-Bild", "Adobe Photoshop file", "PNG-Bild (Portable Network Graphics)", "GIF (Graphics Interchange Format)", "TIFF-Bild"}[/FONT][FONT=Verdana]
[/FONT]
[COLOR=#5E6161][FONT=Verdana]--Get the artwork file[/FONT][/COLOR]
[FONT=Verdana][B]set[/B] [COLOR=#4f8f00]theFolder[/COLOR] [B]to[/B] [COLOR=#012fbe][B]choose folder[/B][/COLOR] [COLOR=#012fbe]with prompt[/COLOR] "Ordner wählen"[/FONT]
[FONT=Verdana][B]tell[/B] [COLOR=#0433ff][I]application[/I][/COLOR] "Finder"[/FONT]
[FONT=Verdana]    [B]set[/B] [COLOR=#4f8f00]theFiles[/COLOR] [B]to[/B] [B]every[/B] [COLOR=#0433ff][I]file[/I][/COLOR] [B]of[/B] [COLOR=#4f8f00]theFolder[/COLOR] [B]whose[/B] [COLOR=#012fbe][I]kind[/I][/COLOR] [B]is[/B] [B]in[/B] [COLOR=#4f8f00]openTypes[/COLOR][/FONT]
[FONT=Verdana][B]end[/B] [B]tell[/B][/FONT]
[FONT=Verdana]
[/FONT]
[FONT=Verdana][COLOR=#4F8F00]runConversion[/COLOR][COLOR=#000000]([/COLOR][COLOR=#4F8F00]theFiles[/COLOR][COLOR=#000000])
......[/COLOR][/FONT]


Die openTypes musst du an deine Bedürfnisse anpassen, die gängigsten Formate habe ich bereits eingetragen.
 
Vielen Dank für deine Antwort.

Ich bekomme leider nach wie vor die Fehlermeldung das die Variable ImageFile nicht definiert sei.
Hast du noch eine Idee?

Nur so nebenbei: Wie sind die openTypes anzugeben?
 
Bin kein Experte, aber vor langer Zeit hab ich mir selbst mal ein Skript gebastelt, das ausgewählte Fotos schnell auf eine bestimmte Größe reduziert und als optimiertes JPEG abspeichert.
Allerdings hab ich das Skript als App gespeichert und in meine Finderleiste oben gezogen, damit ich jeweils bei einzelnen ausgewählten Fotos kurz drauf klicken kann.
Muß also nur noch ein wenig für deine Zwecke abgewandelt werden. Ach ja, und am Ende werden bei mir noch die Tonwertkurve ein wenig angepasst und die Dateigröße optimiert.
Ich hoffe, es hilft ein wenig weiter. Würde mich freuen, dein fertiges Skript hier gepostet zu sehen.

Code:
property allowed_file_types : {"jpg", "jpeg", "png", "gif", "tif", "tiff", "psd", "BMP", "jp2"}

tell application "Finder"
	try
		set this_item to the selection as alias
		set my_Match to false
		set fileExtension to name extension of this_item
		set thisName to name of this_item
		
		-- Überprüfen, ob Dateiendung erlaubt = ob Datei ein Bild ist
		repeat with my_fileExtension_Match in allowed_file_types
			if (my_fileExtension_Match as string) = (fileExtension as string) then
				set my_Match to true
				exit repeat
			end if
		end repeat
		
		if my_Match is false then
			display dialog "Dies ist keine Bilddatei!" buttons "OK" with icon caution default button 1
		else
			try
				-- Bild auf 1400px verkleinern (Upscalen als Option möglich)
				tell application "Image Events"
					launch
					set the target_size to 1400
					set upscaling to false

					set this_image to open this_item					
					set typ to JPEG
					
					copy dimensions of this_image to {current_width, current_height}
					if current_width > target_size or current_height > target_size then
						scale this_image to size target_size
					else if current_height < target_size and current_width < target_size and upscaling is true then
						scale this_image to size target_size
					end if
				end tell
				-- Skaliertes Bild als JPG speichern
				tell application "Finder"
					set new_item to ¬
						(container of this_item as string) & "SKALIERTES_FOTO.jpg"
					save this_image in new_item as typ
					set new_item to quoted form of POSIX path of new_item
				end tell
				tell application "Image Events" to close this_image
				
				-- Überprüfen, ob die Shift-Taste gedrückt wird (mit dem Programm "keys")
				delay 1				
				set path_to_resource to POSIX path of "/Users/tgy/Applications/keys"
				set modkey_shift to (do shell script quoted form of path_to_resource & " shift")
				
				-- Kontrastanpassung des Bildes mit dem Programm "ImageMagick"
				-- "Shift"-Taste gedrückt halten, um Kontrastanpassung zu überspringen
				
				if modkey_shift is not "1" then
					set mycommand to "/Users/tgy/Applications/ImageMagick-6.8.7/bin/convert " & new_item & " -contrast-stretch 0  " & new_item
					
					do shell script "MAGICK_HOME=\"/Users/tgy/Applications/ImageMagick-6.8.7\";export PATH=$MAGICK_HOME/bin:$PATH; export DYLD_LIBRARY_PATH=\"$MAGICK_HOME/lib\";" & mycommand
				end if
				
				-- Dateigröße des JPEG-Bildes mit dem Programm "ImageOptim" optimieren/verkleinern
				do shell script "/Applications/ImageOptim.app/Contents/MacOS/ImageOptim 2>/dev/null " & new_item
			end try
		end if
	end try
end tell
 
Hier nochmal der ganze Code:

Code:
[B]property [/B][COLOR=#4F8F00][FONT=Verdana]openTypes[/FONT][/COLOR][FONT=Verdana] : {"JPEG-Bild", "Adobe Photoshop file", "PNG-Bild (Portable Network Graphics)", "GIF (Graphics Interchange Format)", "TIFF-Bild"}[/FONT][FONT=Verdana]
[/FONT]
[COLOR=#5E6161][FONT=Verdana]--Get the artwork file[/FONT][/COLOR]
[FONT=Verdana][B]set[/B] [COLOR=#4f8f00]theFolder[/COLOR] [B]to[/B] [COLOR=#012fbe][B]choose folder[/B][/COLOR] [COLOR=#012fbe]with prompt[/COLOR] "Ordner wählen"[/FONT]
[FONT=Verdana][B]tell[/B] [COLOR=#0433ff][I]application[/I][/COLOR] "Finder"[/FONT]
[FONT=Verdana]    [B]set[/B] [COLOR=#4f8f00]theFiles[/COLOR] [B]to[/B] [B]every[/B] [COLOR=#0433ff][I]file[/I][/COLOR] [B]of[/B] [COLOR=#4f8f00]theFolder[/COLOR] [B]whose[/B] [COLOR=#012fbe][I]kind[/I][/COLOR] [B]is[/B] [B]in[/B] [COLOR=#4f8f00]openTypes[/COLOR][/FONT]
[FONT=Verdana][B]end[/B] [B]tell[/B][/FONT]
[FONT=Verdana]
[/FONT]
[COLOR=#4F8F00][FONT=Verdana]runConversion[COLOR=#000000]([/COLOR]theFiles[COLOR=#000000])[/COLOR][/FONT][/COLOR]
[FONT=Verdana]
[/FONT]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000][B]on[/B] [/COLOR][COLOR=#0433ff][B]open [/B][/COLOR]someFiles[/FONT][/COLOR]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000]    [/COLOR]runConversion[COLOR=#000000]([/COLOR]someFiles[COLOR=#000000])[/COLOR][/FONT][/COLOR]
[FONT=Verdana][B]end[/B] [COLOR=#0433ff][B]open[/B][/COLOR][/FONT]
[FONT=Verdana]
[/FONT]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000][B]on[/B] [/COLOR]runConversion[COLOR=#000000]([/COLOR]theItems[COLOR=#000000])[/COLOR][/FONT][/COLOR]
[COLOR=#012FBE][FONT=Verdana][COLOR=#000000]    [B]set[/B] [/COLOR][COLOR=#4f8f00]saveFolder[/COLOR][COLOR=#000000] [B]to[/B] [/COLOR][B]choose folder[/B]with prompt[COLOR=#000000] "Save resized pictures where?" [B]without[/B] [/COLOR]multiple selections allowed[COLOR=#000000] [B]and[/B] [/COLOR]invisibles[/FONT][/COLOR]
[COLOR=#012FBE][FONT=Verdana][COLOR=#000000]    [/COLOR][B]display dialog[/B][COLOR=#000000] "Max height?" [/COLOR]default answer[COLOR=#000000] "600"[/COLOR][/FONT][/COLOR]
[FONT=Verdana]    [B]set[/B] [COLOR=#4f8f00]newWidth[/COLOR] [B]to[/B] ([COLOR=#5730be]text returned[/COLOR] [B]of[/B] [B]the[/B] [COLOR=#812fdc]result[/COLOR]) [B]as[/B] [COLOR=#0433ff][I]integer[/I][/COLOR][/FONT]
[FONT=Verdana]    [B]tell[/B] [COLOR=#0433ff][I]application[/I][/COLOR] "Image Events"[/FONT]
[COLOR=#0433FF][FONT=Verdana][COLOR=#000000]        [/COLOR][B]launch[/B][/FONT][/COLOR]
[FONT=Verdana]        [B]set[/B] [COLOR=#4f8f00]newHeight[/COLOR] [B]to[/B] 0[/FONT]
[FONT=Verdana]        [B]if[/B] ([COLOR=#0433ff][B]count[/B][/COLOR] [COLOR=#0433ff][I]items[/I][/COLOR] [B]of[/B] [COLOR=#4f8f00]theItems[/COLOR]) [B]is[/B] [B]greater than[/B] 0 [B]then[/B][/FONT]
[FONT=Verdana]            [B]repeat[/B] [B]with[/B] [COLOR=#4f8f00]anItem[/COLOR] [B]in[/B] [COLOR=#4f8f00]theItems[/COLOR][/FONT]
[FONT=Verdana]                [B]set[/B] [COLOR=#4f8f00]anItem[/COLOR] [B]to[/B] [COLOR=#4f8f00]anItem[/COLOR] [B]as[/B] [COLOR=#0433ff][I]alias[/I][/COLOR][/FONT]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000]                [B]set[/B] [/COLOR]imageFile[COLOR=#000000] [B]to[/B] [/COLOR][COLOR=#0433ff][B]open[/B][/COLOR]anItem[/FONT][/COLOR]
[FONT=Verdana]                [B]copy[/B] [COLOR=#812fdc]dimensions[/COLOR] [B]of[/B] [COLOR=#4f8f00]imageFile[/COLOR] [B]to[/B] {[COLOR=#4f8f00]width[/COLOR], [COLOR=#4f8f00]height[/COLOR]}[/FONT]
[FONT=Verdana]                [B]set[/B] [COLOR=#4f8f00]ratio[/COLOR] [B]to[/B] ([COLOR=#4f8f00]height[/COLOR] / [COLOR=#4f8f00]width[/COLOR])[/FONT]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000]                [B]set[/B] [/COLOR]newHeight[COLOR=#000000] [B]to[/B] ([/COLOR]ratio[COLOR=#000000] * [/COLOR]newWidth[COLOR=#000000]) [B]as[/B] [/COLOR][COLOR=#0433ff][I]integer[/I][/COLOR][/FONT][/COLOR]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000]                [B]if[/B] [/COLOR]newHeight[COLOR=#000000] > [/COLOR]newWidth[COLOR=#000000] [B]then[/B][/COLOR][/FONT][/COLOR]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000]                    [/COLOR][COLOR=#0433ff][B]scale [/B][/COLOR]imageFile[COLOR=#0433ff]to size[/COLOR]newHeight[/FONT][/COLOR]
[FONT=Verdana]                [B]else[/B][/FONT]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000]                    [/COLOR][COLOR=#0433ff][B]scale [/B][/COLOR]imageFile[COLOR=#0433ff]to size[/COLOR]newWidth[/FONT][/COLOR]
[FONT=Verdana]                [B]end[/B] [B]if[/B][/FONT]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000]                [/COLOR][COLOR=#0433ff][B]save [/B][/COLOR]imageFile [COLOR=#0433ff]as [/COLOR][COLOR=#5d3292]JPEG [/COLOR][COLOR=#0433ff]in [/COLOR]saveFolder[/FONT][/COLOR]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000]                [/COLOR][COLOR=#0433ff][B]close [/B][/COLOR]imageFile[/FONT][/COLOR]
[FONT=Verdana]            [B]end[/B] [B]repeat[/B][/FONT]
[FONT=Verdana]        [B]else[/B][/FONT]
[FONT=Verdana]            [COLOR=#012fbe][B]display dialog[/B][/COLOR] "Nothing to convert."[/FONT]
[FONT=Verdana]        [B]end[/B] [B]if[/B][/FONT]
[FONT=Verdana]    [B]end[/B] [B]tell[/B][/FONT]
[COLOR=#4F8F00][FONT=Verdana][COLOR=#000000][B]end[/B] [/COLOR]runConversion[/FONT][/COLOR]
[FONT=Verdana]
[/FONT]

Sollte so gehen. Die openTypes findest du heraus, indem du das Informationsfenster (cmd+I) des Bildes öffnest. Dort steht das als "Art" im Abschnitt "Allgemein".
 
Hallo.

Danke nochmal an euch zwei. Der Skriptvorschlag von Pill hat nun nach dem einfügen von ein paar fehlenden Leerzeichen funktioniert. Ich werde jetzt mal vergleichen um raus zu finden wo der Unterschied ist und dann noch ein bisschen weiter anpassen.
Auch an TGY einen herzlichen Dank, ich werde das mal druchschauen ob ich davon etwas gebrauchen kann.

Ich hoffe ich darf nochmal auf euch zurück kommen wenn es noch Probleme gibt.

vg
 
Zurück
Oben Unten