I keep getting a prompt to grant access to a folder when I’m trying to save Excel files using AppleScript. I’ve tried several different ways to save a workbook without any luck. Googling leads to lots of hits for fixing this issue with OPENING a file (which I got to work) but I haven’t found anything fixing this issue when it occurs while SAVING a file.
set hfsPath to (path to desktop as string) & "1_Image Searches:11039:11039 Image Status v7:11039 Testing_Found Images.xlsx"
--Converting test path
set posixPath to POSIX path of hfsPath
--Trying different ways to save file
tell application "Microsoft Excel"
tell active workbook
--Saving posix path
--save in posixPath
--Saving one way with hfs path
--save workbook as filename hfsPath
--Saving another way with hfs path
save in hfsPath
end tell
end tell
set hfsPath to (path to desktop as text) & "found_images.xlsx"
tell application "Microsoft Excel"
save front workbook in hfsPath
end tell
FYI, you don’t have to put every command in its own tell block. The only ‘tell’ that you need here is the application tell. Conceptually, you’re telling the application to save the workbook rather than telling the workbook to save itself.
Also, I don’t think that you need to work with ‘posix path’. I don’t but I’m using an older OS. In general, posix path is intended for use when running shell scripts, so unless there is a specific requirement for that style of reference, you can avoid making the change.
It works without the grant access dialog if I save it on the desktop but it will give me the grant access dialog if I try to save it in a new folder. I’m wondering if there is any way to get the grant access dialog to go away if I’m saving the Excel file to a new folder?
--Getting the path to the user's desktop folder
set desktop_folder to path to desktop as text
set promo_number to "1234"
--Creating the 1_Image_Searches folder on the user's desktop if it doesn't already exist
if not my CheckForFolder(desktop_folder & promo_number & "_Image Searches" as text) then
tell application "Finder"
make new folder at folder (desktop_folder as alias) with properties {name:(promo_number & "_Image Searches" as text)}
end tell
end if
set image_searches_folder to desktop_folder & promo_number & "_Image Searches" & ":" as text
--The path to the excel file
set hfsPath to image_searches_folder & "found_images.xlsx"
--Trying different ways to save file
tell application "Microsoft Excel"
save front workbook in hfsPath
end tell
------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------
--FUNCTIONS
------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------
--FUNCTION TO CHECK IF A FOLDER EXISTS
on CheckForFolder(thisFolder)
tell application "Finder"
return (exists folder thisFolder)
end tell
end CheckForFolder
It worked to make the grant access dialog go away.
set sd to path to startup disk
tell application id "com.microsoft.Excel" -- Microsoft Excel
try
close sd -- will error
end try
end tell
--Getting the path to the user's desktop folder
set desktop_folder to path to desktop as text
--Test promo number
set promo_number to "55543"
--Creating the 1_Image_Searches folder on the user's desktop if it doesn't already exist
if not my CheckForFolder(desktop_folder & promo_number & "_Image Searches" as text) then
tell application "Finder"
make new folder at folder (desktop_folder as alias) with properties {name:(promo_number & "_Image Searches" as text)}
end tell
end if
set image_searches_folder to desktop_folder & promo_number & "_Image Searches" & ":" as text
--The path to the excel file
set hfsPath to image_searches_folder & "found_images.xlsx"
--Trying different ways to save file
tell application "Microsoft Excel"
save front workbook in hfsPath
end tell
------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------
--FUNCTIONS
------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------
--FUNCTION TO CHECK IF A FOLDER EXISTS
on CheckForFolder(thisFolder)
tell application "Finder"
return (exists folder thisFolder)
end tell
end CheckForFolder