Sure, it’s very ugly but gets the job done.
You’ll need to replace the strings on Step 1 for them to match your setup and also create/delete some
else if statements on Step 3 accordingly.
There are a few comments on the code to help you along the way.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- 1. Sets the strings for search and replace. I took tree sets for me as I sync one OneDrive and two Sharepoint shared documents folders
-- A. My Personal OneDrive
set stringODL to "https://company-my.sharepoint.com/personal/myuser/Documents/"
set newStringODL to "file:///Users/user/OneDrive - Company/"
-- B. Sharepoint Shared Documents folder tor Team 1
set stringTeam1 to "https://company.sharepoint.com/sites/team1/Shared Documents/"
set newTeam1 to "file:///Users/user/Company/Team1/"
-- C. Sharepoint Shared Documents folder tor Team 2
set stringTeam2 to "https://company.sharepoint.com/sites/Team2/Shared Documents/"
set newStringTeam2 to "file:///Users/user/Company/Team2/"
-- 2. Fetches the full name property for the current opened file (a full path)
tell application "Microsoft Word"
set originalURL to full name of active document
-- If you open a local file that is not on an OneDrive nor Sharepoint folder, then the full path will not start with https
if originalURL does not start with "https" then
set originalURL to POSIX path of originalURL
-- 3. Find and Replace: will look for the initially defined strings and replace them with the provided new paths
-- 3.1. Will use this handler from Find and Replace in AppleScript from Stack Overflow (https://stackoverflow.com/questions/28115085/find-and-replace-in-applescript)
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to subject as text
set text item delimiters of AppleScript to prevTIDs
-- 3.2. The if statements for replacing the beggining of the path to the defined variables on Step 1
if originalURL starts with stringODL then
set newURL to replaceText(stringODL, newStringODL, originalURL)
else if originalURL starts with stringTeam1 then
set newURL to replaceText(stringTeam1, newTeam1, originalURL)
else if originalURL starts with stringTeam2 then
set newURL to replaceText(stringTeam2, newStringTeam2, originalURL)
-- If the originalURL variable is already a local path, it should begin with "/Users" so it will just preppend a file:// to the originalURL
originalURL starts with "/Users"
set newURL to "file://" & originalURL