Skip to content

Instantly share code, notes, and snippets.

@mipmip
Created December 7, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mipmip/0e2057cc6fc0894fa4af898a1defee94 to your computer and use it in GitHub Desktop.
Save mipmip/0e2057cc6fc0894fa4af898a1defee94 to your computer and use it in GitHub Desktop.
Insert Images in Word Document adds image title as heading inserts a page break
on basename(thePath) -- Requires POSIX path
if thePath ends with "/" then
set nameIndex to -2
else
set nameIndex to -1
end if
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set thePath to text item nameIndex of thePath
set AppleScript's text item delimiters to ASTID
return thePath
end basename
on remove_extension(this_name)
if this_name contains "." then
set this_name to ¬
(the reverse of every character of this_name) as string
set x to the offset of "." in this_name
set this_name to (text (x + 1) thru -1 of this_name)
set this_name to (the reverse of every character of this_name) as string
end if
return this_name
end remove_extension
tell application "Microsoft Word"
if (count of documents) is greater than or equal to 1 then
tell document 1
set myFolder to (choose folder)
tell application "Finder" to set myFiles to files of myFolder as alias list
repeat with aFile in myFiles
set theFile to my basename(POSIX path of aFile)
set theFileHeader to my remove_extension(theFile)
insert text theFileHeader & return at end of text object of selection
make new inline picture at text object of selection with properties {file name:aFile as text, save with document:true}
insert break at text object of selection break type section break next page
end repeat
end tell
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment