mipmip
12/7/2016 - 8:51 PM

Insert Images in Word Document adds image title as heading inserts a page break

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