How to get rid of the "Grant File Access" dialog in Word 2021+AppleScript when inserting a picture

I use AppleScript to edit MS Word documents (MS Word2021+macOS Ventura 13.5), I want to insert pictures into the document through script:

tell application “Microsoft Word”
make new inline picture at text object of selection with properties ¬
{file name:“image.png”, link to file: false , save with document: true}
end tell

However ,running Script, When inserting a picture ,a “Grant File Access” dialog window pops up,need to select and confirm files to grant access, in this way, automated batch operations cannot be realized.so,How to get rid of the “Grant File Access” dialog?

You can’t totally get rid of this dialog, unfortunately.

Several years ago, Microsoft Office adopted “sandboxing” - a security measure which, among other things, requires user to perform an explicit manual action in order to access certain locations for the first time. Sandboxing is mandatory for products sold on Mac App Store so I assume that’s why Microsoft implemented it in the first place.

And yes, sandboxing is detrimental to certain automation operations. Naturally, I strongly dislike sandboxing and believe that users at least have to have an option to disable it.

As a workaround, you can first access you known picture folders manually from Word, after which you shouldn’t get the ‘Grant Access’ dialog again. This approach is, obviously, of no use if you don’t have a set of known picture folders.

P.S. If I’m missing something and there are ways to get around the sandboxing restrictions I’ll be glad to be corrected.

You will have to use GUI scripting to click the allow button.

Thank you for your reply, based on your reply, I did some testing, but still pop-up dialog, requiring authorization for each image one by one…So, currently it seems that as @robertfern said, I will have to use GUI scripting to click the button…

In addition, there is another question. after authorization, where is the authorization information stored? How to edit or reset them?

Thanks for your reply, But there is a question,When I use “make new inline picture at…” script to insert a picture, where should the GUI script for clicking the button be added?

Can you post a sample script that actually works

If you’re embedding your images in your Word documents, the easiest way is to make a copy of the image file in a trusted folder.

Assuming there is a document open in Word, the cursor is in the main text, and an image file is selected in Finder:

set tempFolder to path to temporary items

tell application "Finder"
	set tempSubFolder to "Office PDF Temp"
	set theDest to (tempFolder & tempSubFolder) as string
	if not (exists folder theDest) then make new folder at tempFolder with properties {name:tempSubFolder}
	set theFile to selection as string
	copy theFile to theDest
end tell

tell application id "MSWD"
	set insertionPoint to (text object of paragraph 1 of selection)
	select insertionPoint
	set newPict to (make new inline picture at before insertionPoint with properties {file name:theFile, save with document:true})
end tell
1 Like

Here is a script that works.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on run argv
	local cPath, pid
	if class of argv is list then
		if (count of argv) > 0 then
			if (item 1 of argv) is in {"WORD"} then -- this is the trigger word to start a sub-thread to do the GUI part
				getPermission()
				return
			end if
		end if
	end if
	set cPath to POSIX path of (path to me)
	set pid to do shell script "/usr/bin/osascript '" & cPath & "' WORD &>/dev/null & echo $!" -- this will run this script again to do the GUI stuff
	tell application "Microsoft Word"
		make new inline picture at text object of selection with properties {file name:"Mac SSD:Users:robert:Documents:Widgets:Analog Clock.widget:Contents:Resources:AnalogClock:Small Face.png", save with document:true}
	end tell
end run

on getPermission()
	local c
	tell application "System Events" -- do GUI stuff here
		tell application process "Word"
			repeat with c from 1 to 20
				if exists window "Grant File Access" then exit repeat
				delay 0.5
			end repeat
			if c = 20 then return false -- timeout occured,  window not found
			tell window "Grant File Access"
				if exists button "Select..." then
					click button "Select..."
				else
					return false
				end if
				repeat with c from 1 to 20
					if exists (sheet 1 whose description is "open") then exit repeat
					delay 0.5
				end repeat
				if c = 20 then return false -- timeout occured,  window not found
				tell (sheet 1 whose description is "open")
					if exists button "Grant Access" then
						click button "Grant Access"
					else
						return false
					end if
				end tell
			end tell
		end tell
	end tell
end getPermission

This is a method I use to do multi-threading in AppleScript

1 Like

“the easiest way is to make a copy of the image file in a trusted folder.”

I agree with your suggestion,Thank you very much. Yes, There is indeed such a folder in my desktop folder, This folder is a test folder I created when I was learning scripts. It stores all the pictures to be inserted. Currently, as long as a new word document and this test folder, they are all on the desktop, Even if I put new pictures in the test folder, or rename the test folder, the authorization dialog will not appear. But I didn’t pay attention to what I did to establish this trust relationship. so, my question is how to create a folder that word documents trusted? and, how to reset this trust relationship?

Thank you for your detailed reply,I am a beginner,the following is my code,The purpose is to insert the corresponding picture of the same name as the caption in the caption position of the document, based on your reply, I try to integrate the code and then test it,if possible, please give me more advice, Thanks again.

tell application "Microsoft Word"
	open file "Macintosh HD:Users:smith:Desktop:mydoc.docx"
	active document
	set indexpic to 1
	set selFind to find object of selection
	set foundIt to true
	repeat while foundIt
		set selection start of selection to 0
		set selection end of selection to 0
		set findtext to ("Picture-" & indexpic)
		set foundIt to execute find selFind find text findtext wrap find find stop with match forward without find format
		tell application "Finder"
			set checkfile to exists file ("Macintosh HD:Users:smith:Desktop:test:" & findtext & ".png")
		end tell
		
		if foundIt is true and checkfile is true then
				make new inline picture at text object of selection with properties ¬
					{file name:"Macintosh HD:Users:smith:Desktop:test:" & findtext & ".png", link to file:false, save with document:true}
		end if
		set indexpic to indexpic + 1
	end repeat
end tell