AppleScript für lange Texte in den iPod-Notizen

mekkablue

mekkablue

Aktives Mitglied
Thread Starter
Dabei seit
28.01.2005
Beiträge
737
Reaktionspunkte
48
Ich hab für diejenigen unter euch, die ein bisschen Literatur am iPod schätzen, ein kleines AppleScript geschrieben, das eine lange Textdatei in iPod-gerechte, verlinkte Unicode-Texthäppchen teilt. Wer schon mal einen Projekt-Gutenberg-Text am iPod lesen wollte, weiß, wovon ich spreche.

Wie man's benutzt, sieht man in diesem Demo-Video.

Wer will, kann den folgenden Code kopieren, in ein Skripteditor-Dokument einfügen und dann als "Programm" speichern, dann funktioniert auch das Drag&Drop (auch mit mehreren Textdateien gleichzeitig):

Code:
-- localisation strings
property teil_name : "Teil "
property previous_text : "[zurück]"
property next_text : "[weiter]"
property choosetextfile_text : "Textdatei auswählen:"
property choosefolder_text : "Ordner, in dem die Teile gespeichert werden sollen:"

-- data stuff
property chunk_length : 1500
property number_of_zeros : 2



on open myfiles
	repeat with this_file in myfiles
		my process_file(this_file)
	end repeat
end open

on run
	set myfile to choose file with prompt choosetextfile_text
	my process_file(myfile)
end run

on process_file(myfile)
	-- Text einlesen
	set myfile_id to open for access myfile
	set myfulltext to read myfile_id as text
	close access myfile_id
	
	-- feststellen, wieviele Nullen wir für den Namen brauchen (iPod ordnet sonst 1 - 10 - 11 - 2 - 3 - 4...)
	set number_of_zeros to length of ((length of myfulltext) div chunk_length as string)
	
	-- Zielort festlegen	
	tell application "Finder" to set myfilename to name of myfile
	set mypath to choose folder with prompt "[" & (myfilename) & ":] " & choosefolder_text
	
	my split_text(mypath, myfulltext)
end process_file

on split_text(mypath, myfulltext)
	set mycount to 0
	
	repeat while ((length of myfulltext) > chunk_length)
		set mycount to mycount + 1
		set mypos to chunk_length
		set mychar to ""
		
		-- dafür sorgen, dass wir bei einem Leerzeichen trennen
		repeat until mychar is " "
			set mypos to mypos + 1
			set mychar to character mypos of myfulltext
		end repeat
		
		-- aktuellen Teil abzwacken vom Resttext
		set mychunk to text 1 thru mypos of myfulltext
		set myfulltext to text (mypos + 1) thru -1 of myfulltext
		my writetofile(mychunk, mycount, mypath, false)
	end repeat
	
	-- letztes Stück ist kürzer
	writetofile(myfulltext, mycount + 1, mypath, true)
end split_text

on turn_into_zerostring(mycount)
	set mycountstring to text -number_of_zeros thru -1 of ("0000000" & mycount)
	return mycountstring
end turn_into_zerostring

on writetofile(mychunk, mycount, mypath, lastone)
	-- Wenn nötig den Previous-Link am Anfang einfügen
	if mycount > 1 then
		set mychunk to "<a href=\"" & teil_name & (my turn_into_zerostring(mycount - 1)) & "\">" & previous_text & "</a>" & return & return & "…" & mychunk
	end if
	
	-- Wenn nötig den Next-Link am Ende einfügen
	if lastone is false then
		set mychunk to mychunk & "…" & return & return & "<a href=\"" & teil_name & (my turn_into_zerostring(mycount + 1)) & "\">" & next_text & "</a>"
	end if
	
	-- UTF8-Header
	set mychunk to "<?xml encoding=\"UTF8\"?>" & return & mychunk
	
	-- Teildatei schreiben
	tell application "Finder"
		set my_filename to "" & teil_name & (my turn_into_zerostring(mycount))
		set myfullfilepath to "" & (mypath as text) & my_filename
		
		set myref to open for access file myfullfilepath with write permission
		write mychunk to myref as «class utf8»
		close access myref
	end tell
end writetofile

Viel Spaß!
 
Zuletzt bearbeitet von einem Moderator:
Zurück
Oben Unten