Maybe some other people have ideas on how to improve this. I just wrote it over the past couple days. In its current state, it must be run outside of TeXShop. Does anyone know who I could implement this to work directly from TeXShop as a macro?
For the impatient...skip the rest and just download the AppleScript app: http://dl.dropbox.com/u/47530119/texshop_insert%20graphic.app.zip
For the visual learners...youtube video: http://www.youtube.com/watch?v=MbaznQ2VY54
What does it do? Check for appropriate packages in the preamble, adding necessary ones (if not already there) Offer a few options to set up the image Add graphics code at cursor in TeXShop UPDATE Added bloodworks suggestion to code for usage directly in TeXShop. I did not know how to do this before!
-- Applescript
(*
Version 1
Script written by Jonathan Komar on April 13, 2012
*)
--insComplete contains all code entered at cursor position
--insertatCursor enters code at cursor in texshop
--insertPackage handler inserts necessary packages in the preamble, only if not already present
tell application "Finder"
--WHAT DO I DO?
(*I get the picture location and request information on how that picture should be formatted.*)
set myPicturePX to POSIX path of (choose file with prompt "choose a graphics file to import, must be jpg, png")
set scale to text returned of (display dialog "Do you want to scale the image?" default answer "width=\\textwidth")
set angle to text returned of (display dialog "Do you want to set the angle of the image? " default answer "angle=0")
set picName to do shell script "filename=$(basename $" & myPicturePX & ")" & "; filename=${filename%.*}; echo $filename"
end tell
tell application "TeXShop"
-- get the front document
set thisDoc to the front document
global thisDoc
set docLocPX to (path of thisDoc) as string
--global docLocPX
set defaultTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set docName to last text item of (docLocPX as text)
set AppleScript's text item delimiters to defaultTIDs
global docName
set docName to do shell script "filename=$(basename $" & docLocPX & ")" & "; filename=${filename%.*}; echo $filename"
set docLocAL to do shell script "echo `pwd " & docLocPX & "`"
end tell
--THIS STUFF SHOULD BE DONE FIRST
activate "Finder"
set figAns to (choose from list {"No Environment", "Figure", "Wrap Figure"} with prompt "Would you like a figure environment, in order to add a caption?" multiple selections allowed no)
set fboxAns to button returned of (display dialog "Should the image have a border?" buttons {"No", "Yes"} default button "Yes")
if fboxAns is "No" then
--if no fbox, basic setup and pass
set gfxstring to "\\includegraphics[" & scale & ", " & angle & "]{" & picName & "}"
else
--if fbox, add the fbox env to gfxstring and pass
set gfxstring to "\\setlength\\fboxsep{0pt}" & "
" & "\\setlength\\fboxrule{0.5pt}" & "
" & "\\fbox{\\includegraphics[" & scale & ", " & angle & "]{" & picName & "}}"
end if
--SHOULD BE DONE SECOND
--add the figure framework
if figAns as string is "Figure" then
activate "Finder"
set figCaption to button returned of (display dialog "Would you like to include a caption?" buttons {"No", "Yes"} default button "Yes")
if figCaption is "Yes" then
set caption to text returned of (display dialog "Please provide a caption:" default answer picName)
end if
set insComplete to "\\begin{figure}[htb]" & "
" & "\\centering" & "
" & gfxstring & "
" & "\\caption{" & caption & "}" & "
" & "\\label{" & picName & "}" & "
" & "\\end{figure}"
else
--add wrapfigure framework
set figCaption to button returned of (display dialog "Would you like to include a caption?" buttons {"No", "Yes"} default button "Yes")
if figCaption is "Yes" then
set caption to text returned of (display dialog "Please provide a caption:" default answer picName)
set alignAns to (choose from list {"left", "right", "center"} with title "List Title" with prompt "How should the image be aligned?" OK button name "Continue" cancel button name "Stop" without multiple selections allowed) as string
if alignAns is "left" then
set align to "l"
else
if alignAns is "right" then
set align to "r"
else if alignAns is "center" then
set align to "c"
end if
end if
set picPadding to text returned of (display dialog "How much padding should be used?" default answer "5cm")
set insComplete to "\\begin{wrapfigure}{" & align & "}{" & picPadding & "}" & "
" & "\\centering" & "
" & gfxstring & "
" & "\\caption{" & caption & "}" & "
" & "\\label{" & picName & "}" & "
" & "\\end{wrapfigure}"
else
set caption to ""
set alignAns to (choose from list {"left", "right", "center"} with title "List Title" with prompt "How should the image be aligned?" OK button name "Continue" cancel button name "Stop" without multiple selections allowed) as string
if alignAns is "left" then
set align to "l"
else
if alignAns is "right" then
set align to "r"
else if alignAns is "center" then
set align to "c"
end if
end if
set picPadding to text returned of (display dialog "How much padding should be used?" default answer "5cm")
set insComplete to "\\begin{wrapfigure}{" & align & "}{" & picPadding & "}" & "
" & "\\centering" & "
" & gfxstring & "
" & "\\caption{" & caption & "}" & "
" & "\\label{" & picName & "}" & "
" & "\\end{wrapfigure}"
end if
end if
my insertatCursor(insComplete, thisDoc) --this actually doees the insert
--THIS SHOULD BE DONE LAST BECAUSE IT WILL CHANGE THE CURSOR LOCATION
------------------for insertPackage use 2 backslashes here, trust me I double checked this
--insertpackage format= {looking for what, before what, posix path of file}
my insertPackage("\\usepackage{graphicx}", "\\begin{document}", docLocPX) --allow for graphics
my insertPackage("\\graphicspath{{./" & docName & "_images/}}", "\\begin{document}", docLocPX) --set path to images
my insertPackage("\\usepackage{wrapfig}", "\\begin{document}", docLocPX) --wrap image env
--my insertPackage("\\usepackage{grffile}", "\\begin{document}", docLocPX) --allow spaces in filenames
my insertPackage("\\DeclareGraphicsExtensions{.pdf,.png,.jpg,.jpeg}", "\\begin{document}", docLocPX) --you have to set jpeg for example
my refreshDoc(thisDoc)
(**********************************************)
on insertPackage(findThis, beforeThis, inThis)
--WHAT DO I DO?
(*I search for a string, if I don't find it, I add it before another specific string. This is useful for writing XeLatex documents where the \\begin{document} would signify the end of the preamble*)
------------------for insertPackage you need 4 backslashes to get just one slash because you need to escape a applescript slash and bash slash
set fileContent to read inThis
if fileContent contains (findThis) then
--string found
else --in this case we will add the missing stuff in front of our string
--"string not found"
--do shell script "sh /Users/Jonathan/scripts/bash/sed_add_line_before_word.sh " & inThis
do shell script "sed '/" & beforeThis & "/ i\\" & "
" & "\\" & findThis & "
' " & inThis & ">$HOME/tmp9999.tmp ; mv $HOME/tmp9999.tmp " & inThis
end if
end insertPackage
on insertatCursor(myString, thisDoc)
tell application "TeXShop"
set selection of thisDoc to myString
try
save thisDoc
end try
end tell
end insertatCursor
on refreshDoc(thisDoc)
--At the end we refresh the front document
tell application "TeXShop"
refreshtext thisDoc
end tell
end refreshDoc
activate "Finder"
--copies image into a folder relative to the tex file as docName_images
tell application "Finder"
--this tricky bastard took me 20 minutes to solve, just needed to put "as alias" on the end
set docPathAL to POSIX file docLocPX as alias
set parentPath to my GetParentPath(docPathAL)
set destinationAL to (my GetParentPath(docPathAL) & docName & "_images:")
set destinationPX to POSIX path of destinationAL
set myPictureAL to POSIX file myPicturePX as alias
try
make new folder at alias parentPath with properties {name:docName & "_images"}
end try
try
--make alias at destination to file myPictureAL
--duplicate myPictureAL to destination
do shell script "ln -s " & myPicturePX & " " & destinationPX
end try
--display dialog "Could not make alias to picture. The TeX document will not be able to find the picture and therefore will not compile correctly."
end tell
on GetParentPath(thefile)
tell application "Finder" to return container of thefile as text
end GetParentPath
This is a script references from the applescript called sed_add_line_before_word.sh which contains:
#!/bin/sh
sed '
/\\begin{document}/ i\
\\usepackage{graphicx}
' $1

-- Applescriptis missing, therefore TeX Shop recognizes the script as text and inserts the code. Steps to load it into TS: 1. open TS 2. Open Macros -> Macros Editor 3. Pushnewin a appropriate catagory 3. cmd-v cmd-c (in textfield) add-- Applescript4. pushSaveand or add a key-shortcut. 5. close Macro Editor 6. choose Macros->thenameofthescript to run it. While it works for me a few errors occur but with no effects. – bloodworks Apr 13 '12 at 14:21