Fischer-Bayern.de
 FAQFAQ   SuchenSuchen   MitgliederlisteMitgliederliste   BenutzergruppenBenutzergruppen   RegistrierenRegistrieren 
 ProfilProfil   Einloggen, um private Nachrichten zu lesenEinloggen, um private Nachrichten zu lesen   LoginLogin 

Matrix-Generator für TexShop

 
Dieses Forum ist gesperrt, du kannst keine Beiträge editieren, schreiben oder beantworten.   Dieses Thema ist gesperrt, du kannst keine Beiträge editieren oder beantworten.    Fischer-Bayern.de Foren-Übersicht -> Pool
Vorheriges Thema anzeigen :: Nächstes Thema anzeigen  
Autor Nachricht
woodpecker
•--->
•--->


Anmeldedatum: 12.10.2010
Beiträge: 150
Wohnort: Roggentin/Rostock

BeitragVerfasst am: 21.11.2010 - 15:45    Titel: Matrix-Generator für TexShop Antworten mit Zitat

Hallo TexShop-Nutzer,

das in TexShop enthaltene Makro zur Erzeugung eines array-Blockes für eine Matrix-Darstellung ist wenig flexibel, da damit immer eine Matrix mit einer festen Zeilen- und Spaltenanzahl erzeugt wird.

Ich habe mir deshalb einen Generator in AppleSkript geschrieben, der die Zeilen- und Spaltenanzahl dynamisch aus dem TexShop-Dokument ermittelt und daraus an die Stelle der eingegebenen Matrix einen entsprechenden array-Block stellt.

Die einzelnen Matrixelemente innerhalb einer Zeile müssen durch mindestens ein Space oder TAB voneinander getrennt sein die einzelnen Zeilen durch mindestens ein Newline.
Das Alignment der Matrix-Elemente wird durch Dialog abgefragt.

Die Anzahl der Elemente in der ersten Zeile bestimmt die Anzahl der Spalten. Kommen in einer der Folgezeilen mehr als 0 aber nicht genauso viele Elemente wie in der ersten Zeile vor, wird das Skript mit einem Fehlerdialog aus dem die fehlerhafte Zeile erkennbar ist abgebrochen.

Das Skript benutzt einige Handler aus meinem stringdll-Skript, das ich noch um einige Funktionen erweitert habe und deshalb hier auch noch mal poste.

Der Generator ist getestet unter Mac OS X Version 10.6.5
Meine TexShop Version ist 2.37

Hier das Skript:

-- Script : Matrix (AppleScript) Stand vom : Sonntag, 21. November 2010 13:24:59
-- Autor : Erich Schmidt

set stringdll to load script file ((path to "docs" as string) & "AppleScript:stringdll.scpt")

set Win to window index 1 of application "TeXShop"
set Dok to document of Win
set NL to ASCII character 13
tell application "TeXShop"
set propt to properties of selection of Dok

set Matrix to content of propt
if Matrix is not "" then
set MarkedRows to 1st item of stringdll's StrCount(Matrix)

set MLines to {}
set MatrixRows to 0
set MatrixCols to 0
repeat with i from 1 to MarkedRows
set xline to Sublines of stringdll into Matrix from i to i
set xline to Substitute of stringdll for " +$" by "" into xline without Globalchg
set xwords to item 2 of stringdll's StrCount(xline)
if MatrixCols is 0 then
set MatrixCols to xwords
else
if MatrixCols is not xwords and xwords is greater than 0 then
set Dtxt to (("Nbr. of cols: " & MatrixCols as string) & NL & "Line " & i as string) & " contains " & xwords & " Elements"
display dialog Dtxt buttons {"Cancel"} default button 1
end if
end if
if xwords is greater than 0 then
copy xline to end of MLines
set MatrixRows to 1 + MatrixRows
end if
end repeat

set alignment to display dialog "Select alignment" buttons {"Left", "Right", "Center"} default button 3
set alignment to alignment as list as string
set alignx to ASCII character ((ASCII number 1st character of alignment) + 32)
set align to ""
repeat with i from 1 to MatrixCols
set align to align & alignx
end repeat

set TexIns to "\\[ " & NL & "\\left(" & NL & "\\begin{array}{" & align & "}" & NL
tell stringdll
repeat with i from 1 to count MLines
set xline to item i of MLines
set xline to Substitute for "[^[:space:]]+[ \\t]" by "& \\\\& " into xline with Globalchg

if i is less than (count MLines) then set xline to Substitute for ".*" by "& \\\\\\\\" into xline without Globalchg
set TexIns to TexIns & xline & NL
end repeat
end tell

set TexIns to TexIns & "\\end{array}" & NL & "\\right)" & NL & "\\]" & NL

activate
stringdll's KeyString(TexIns)
end if
end tell


Und hier die DLL


-- Script : stringdll (AppleScript) Stand vom : Samstag, 20. November 2010 12:15:16
-- Autor : Erich Schmidt

# Regeln: Patternsuche geht nicht über Zeilengrenzen
# Ein Pattern darf kein Slash oder Newline enthalten

# Rueckgabe eines Teilstrings
# spos - Startposition, beginnend bei 1 (beschreibt die Startposition ein Zeichen ausserhalb
# des Strings, wird ein Leerstring zurückgegeben)
# len - Länge des Teilstrings, wenn positiv, beliebig gross
# wenn negativ oder 0 wird die Länge 1 benutzt.

on Substr(Txt, spos, len)
set EndOfFile to my EOFHereDoc()
set Ntxt to do shell script "wc -c <<" & EndOfFile & "
" & Txt & "
" & EndOfFile
set Ntxt to Ntxt as number

if spos is not greater than 0 or spos is greater than Ntxt then return ""
if len is not greater than 0 then set len to 1

set awk to "<<" & EndOfFile & " awk -v S=" & spos & " -v L=" & len & " ' { if( length > S ) { rc=substr($0,S,L) ; nextfile} else S-=length+1}
END {print rc}'
" & Txt & "
" & EndOfFile
set rc to do shell script awk
return rc
end Substr

# Position eines Pattern innerhalb eines Strings Txt
# Das Pattern kann ein beliebiger regulärer Ausdruck sein
# Es wird die Position des ersten Pattern innerhalb des Strings zurückgegeben
on |StrIndex|(Txt, pattern)

if |CheckPattern|(pattern) then
return 0
end if
set EndOfFile to my EOFHereDoc()

set awk to "<<" & EndOfFile & " awk '/" & pattern & "/ { match($0,\"" & pattern & "\");n=RSTART ;s=1;nextfile}
{ idx += length+1 }
END {printf s ? idx+n : 0} '
" & Txt & "
" & EndOfFile
set rc to do shell script awk
return rc as number

end |StrIndex|

# Match eines Pattern in einem String Txt
# Es wird eine Liste zurückgegeben: {POSITION,LAENGE,MATCH}
#
# Beispiele: |Match| for "[X-Z][0-9]{1,2}" into "INDEX3567" ergibt {5, 3, "X35"}
# |Match| for " .*tag" into "Mittwoch Donnerstag" ergibt {9, 11, " Donnerstag"}


on |Match| for pattern into Txt
set NullMatch to {0, 0, ""}
if |CheckPattern|(pattern) then
return NullMatch
end if
set EndOfFile to my EOFHereDoc()
set awk to "<<" & EndOfFile & " awk '/" & pattern & "/ { match($0,\"" & pattern & "\") ;s=1;line=$0;n=RSTART;nextfile }
{ idx += length+1 }
END { printf \"%d %d :%s\", s?idx+n:0,s?RLENGTH:0,substr(line,RSTART,RLENGTH) } '
" & Txt & "
" & EndOfFile

set rc to do shell script awk

if rc is equal to "" then return NullMatch
set Position to 1st word of rc as number
set Lengthx to 2nd word of rc as number
set rc to {Position, Lengthx, Substr(rc, 1 + |StrIndex|(rc, ":"), Lengthx)}
return rc

end |Match|

# Substitute into Txt for old by new with[out] GlobalChg
# Substituiert Pattern old durch new im String mit der Option with GlobalChg (alle)
# oder without GlobalChg (nur das erste)
# Beispiel: Substitute of stringdll for "tag" by "& nacht," into "Dienstag Mittwoch Donnerstag" with GlobalChg
# ergibt: "Dienstag nacht, Mittwoch Donnerstag nacht,"

on Substitute for old by new into Txt given Globalchg:mode
set m to ""
set g to "0"
if mode then
set m to "g"
set g to "1"
end if
set EndOfFile to my EOFHereDoc()
set awk to "<<" & EndOfFile & " awk -v G=" & g & " '{if(G || !n ) n=" & m & "sub(\"" & old & "\", \"" & new & "\");print $0 } '
" & Txt & "
" & EndOfFile
return do shell script awk
end Substitute

# Anzahl Vorkommen von pattern in Txt zählen
# Beispiel: PatternCount of stringdll for "tag" into " Dienstag Mittwoch Donnerstag " ergibt 2
# aber: PatternCount for "D.*tag" into " Dienstag Mittwoch Donnerstag " ergibt 1 !!

on PatternCount for pattern into Txt
if |CheckPattern|(pattern) then
return 0
end if
set EndOfFile to my EOFHereDoc()
set awk to "cat <<" & EndOfFile & " | awk '{ n+=gsub(\"" & pattern & "\", \" \") }
END { printf \"%d\", n } '
" & Txt & "
" & EndOfFile

set rc to do shell script awk
return rc as number
end PatternCount


# Der Handler sendet Tastenanschläge, die auch Umlaute enthalten können
# Zu beachten ist, dass beim Match als Pattern ein Punkt hinter der
# Umlautklasse angegeben werden muss, damit der Umlaut im 3rd item of match erscheint
# Die Länge des Matches ist dann auch 2, sie wird aber hier nicht benutzt
# Wenn zwei Umlaute hintereinander stehen, funktioniert's leider noch nicht
# Beispiel: KeyString("Käsebrötchen")
on KeyString(Txt)

set Delta to 135
set match to |Match| of me for "[äöüÄÖÜ]." into Txt

set pos to 1st item of match
if pos is 0 then
tell application "System Events" to keystroke Txt
else
set uml to 3rd item of match as string
if "ä" is uml then set Delta to 131
set uml to character id ((id of uml) - Delta)
tell application "System Events"
keystroke my Substr(Txt, 1, pos - 1)
keystroke "u" using option down
keystroke uml
set Txt to my Substr(Txt, pos + 2, count Txt)
my KeyString(Txt)
end tell
end if
end KeyString

# Rückgabe einer Liste {LINES,WORDS,BYTES} aus Txt
on StrCount(Txt)
set EndOfFile to my EOFHereDoc()
set rc to do shell script "wc <<" & EndOfFile & "
" & Txt & "
" & EndOfFile
return {1st word of rc as number, 2nd word of rc as number, 3rd word of rc as number}
end StrCount

# Zeilenauswahl aus Txt
# a und b können als Zahl oder Pattern angegeben sein

on Sublines into Txt from a to b
set EndOfFile to my EOFHereDoc()
set range to a & "," & b
set sed to "<<" & EndOfFile & " sed -n ' " & range & "p'" & "
" & Txt & "
" & EndOfFile
return do shell script sed
end Sublines

on EOFHereDoc()
set StrEOF to "Das+ist+ein+Text+der+den+EOF+String+fuer+HereDocuments+liefert.Er+kann+beliebig+verlaengert+werden.Aber+Vorsicht+mit+Sonderzeichen"
set EndOfFile to {}
repeat with k from 1 to ((random number from 15 to 20) as integer)
copy some character of StrEOF to end of EndOfFile
end repeat
return EndOfFile as string
end EOFHereDoc


# check embedded slash and newline
on |CheckPattern|(pattern)

if pattern contains "/" then
display dialog "pattern may not contain slash" buttons {"OK"} default button 1
return true
end if
if pattern contains "
" then
display dialog "pattern may not contain newline" buttons {"OK"} default button 1
return true
end if
return false
end |CheckPattern|



Der Beitrag wurde einmal editiert. Zuletzt am Sonntag, 21. November 2010 15:50:51
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden E-Mail senden
Nishaknapp
•--->
•--->


Anmeldedatum: 16.03.2022
Beiträge: 217

BeitragVerfasst am: 13.06.2022 - 09:09    Titel: Antworten mit Zitat

Why not settling on games that is fun and at the same time your earning. Well it'll make suspense because of the game as well but dude just try it and it gave me hope while pandemic is real rn. THE REALITY OF BACCARAT: A FULFILLING CAREER THAT IS NOT SUITABLE FOR ALL
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden
Beiträge der letzten Zeit anzeigen:   
Dieses Forum ist gesperrt, du kannst keine Beiträge editieren, schreiben oder beantworten.   Dieses Thema ist gesperrt, du kannst keine Beiträge editieren oder beantworten.    Fischer-Bayern.de Foren-Übersicht -> Pool Alle Zeiten sind GMT + 2 Stunden
Seite 1 von 1

 
Gehe zu:  
Du kannst keine Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum nicht antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.


Powered by phpBB © 2001, 2002 phpBB Group
Deutsche Übersetzung von phpBB.de


AppleScript für absolute Starter