﻿(* dm at mayart.de, 2008

Adobe Illustrator CS 3 und CS4
Alle eingebetteten Rasterbilder im aktiven Dokument  als Photoshop-Dateien in einen neuen Ordner speichern.

Das Skript muss im Apple Skripteditor als *.scpt gespeichert und kann
unter Programme > Adobe Illustrator CSx > Vorgaben > Skripte abgelegt werden. 
Nach Neustart von Illustrator findet man es im Menü Datei > Skripten. 

Es geht auch mit dem Skript Menü in OS X (installieren mit AppleScript Utility in Programme > Applescript)
oder besser mit FastScripts: <http://www.red-sweater.com/fastscripts/index.html>.
Hier liegen die Skripte in ~/Library/Scripts/...

*)

tell application "Adobe Illustrator"
	
	if current document = {} then return
	
	set current_doc to current document
	
	set file_path to file path of current_doc
	
	set doc_name to name of current_doc
	
	set base_name to my get_base_name(doc_name)
	
	set parent_folder to my get_parent_folder(file_path)
	
	set new_folder to (my make_new_folder(parent_folder, (base_name & "_img"))) as string
	
	set raster_items to every raster item of current_doc
	
	if raster_items is {} then return
	
	try
		repeat with i from 1 to number of items in raster_items
			
			set this_item to item i of raster_items
			
			my save_raster_items(this_item, current_doc, new_folder, base_name, (i as text))
			
		end repeat
		
	on error error_message
		display dialog error_message
	end try
	
end tell


on save_raster_items(raster_item, current_doc, new_folder, base_name, item_counter)
	
	tell application "Adobe Illustrator"
		
		set s to mvalue_a of matrix of raster_item
		
		if s is 0.0 then set s to mvalue_b of matrix of selection
		
		set m_res to (72 / s)
		
		set selection to raster_item
		
		copy
		
		set color_space to color space of current document
		
		set new_dok_ref to make new document with properties {color space:color_space}
		
		paste
		
		tell new_dok_ref
			
			try
				set clipping of every path item to false
			end try
			
			set target_item to raster item 1
			
			try
				delete every path item
			end try
			
			set matrix of target_item to get identity matrix
			
			set h to height of target_item
			
			translate target_item delta y h
			
			set new_file_path to (new_folder & base_name & "_" & item_counter & ".psd") as string
			
			export to new_file_path as ¬
				Photoshop with options {class:Photoshop export options ¬
				, resolution:72.0, color space:color_space ¬
				, antialiasing:false, editable text:false, embed ICC profile:true ¬
				, maximum editability:true, warnings:true, write layers:false}
			
			close saving no
			
		end tell
		
	end tell
	
	do shell script "sips -s dpiHeight " & m_res & " -s dpiWidth " & m_res & " " & (quoted form of POSIX path of new_file_path)
	
	tell application "Finder"
		activate
		reveal folder new_folder
	end tell
	
end save_raster_items


on get_base_name(the_name)
	
	set x to (offset of "." in ((reverse of characters of the_name) as string))
	
	set l to length of the_name
	
	if the_name starts with "." then
		set the_base to "." & (characters 2 through (l - x) of the_name) as string
	else
		set the_base to characters 1 through (l - x) of the_name as string
	end if
	
	return the_base
	
end get_base_name


on get_parent_folder(this_item)
	
	set this_item to this_item as text
	
	if this_item ends with ":" then
		
		set cc to count characters of this_item
		
		set this_item to characters 1 through (cc - 1) of this_item as string
		
	end if
	
	set n to every character of this_item as list
	
	set r to reverse of n
	
	set x to (offset of ":" in (r as string)) - 1
	
	set cc to count characters of this_item
	
	set parent_folder to characters 1 through (cc - x) of this_item as string
	
	return parent_folder
	
end get_parent_folder



on make_new_folder(parent_folder, folder_name)
	
	set new_folder to parent_folder & folder_name
	set n to 0
	
	tell application "Finder"
		
		if (exists of folder new_folder) = true then
			
			repeat
				
				set n to n + 1
				
				set f to parent_folder & folder_name & "_" & n
				
				if (exists of folder f) = false then exit repeat
				
			end repeat
			
			set new_folder to (make new folder at (parent_folder as alias) with properties {name:(folder_name & "_" & n)}) as alias
			
		else
			
			set new_folder to (make new folder at (parent_folder as alias) with properties {name:folder_name}) as alias
			
		end if
		
		return new_folder
		
	end tell
	
end make_new_folder

