aliases - change/update pathname for original target files & folders?

Is there any way
to change an Alias’ “original item” (the files/folders’-paths)
referencing target files/folders?

… for example:

the alias’ original (outdated) file path
was
“/Volumes/VolumeA/dummy.png”

i need to change it to e.g.
“/Users/my_mac/Documents/Sources/VolumeA/dummy.png”


i tried & looked up postings related to “alias - scripting”
but could not find a solution yet.


i was able to identify the original paths
using examples from macscripter
( … http://macscripter.net/search.php?action=tag_search&tag=alias … )

e.g.

tell application “Finder”
set theSelection to selection – get and check the selected files in the frontmost finder window
repeat with aFile in theSelection – loop through the selected files
if class of aFile is alias file then – choose and procede with all aliases
get original item of aFile – the linked file name AND path (Applescript formatted)

but -
how may i change the path ?
(“set original item to …” and several other code variations did not seem to work)


i hope there is a solution out there -
any help or hint is highly appreciated :slight_smile:

Here’s how…

set theAlias to (path to desktop as text) & "test2.txt alias"
set newAliasPath to (path to desktop as text) & "test.txt"

tell application "Finder"
	set original item of file theAlias to file newAliasPath
end tell

Hi,

do you need a POSIX path (“/Volumes/VolumeA/dummy.png”) or a HFS path (“VolumeA:dummy.png”)?
To process files in AppleScript you need HFS, with the shell POSIX.

This sample code shows how to get both


tell application "Finder"
	set theSelection to selection -- get and check the selected files in the frontmost finder window 
	repeat with aFile in theSelection -- loop through the selected files
		if class of aFile is alias file then -- choose and procede with all aliases 
			set HFSfilePath to original item of aFile as text
			set POSIXfilePath to POSIX path of (original item of aFile as text) --  the linked file name AND path (Applescript formatted)
		end if
	end repeat
end tell

dear regulus & StefanK -

thanks a million times for your immediate replies & your suggestions!

great to have a POSIX solution.

i assume that it might be more easy to use applescript
in combination with automator to check/change my 5000+ aliases
that “only” need a change for the first bits of the links
replacing e.g. the “/Users/my_mac/Documents/Sources/” with “/Volumes/” - chunks.

will post a solution based on your help* as soon as i succeeded
thanks a lot again

  • will try them first thing tomorrow morning
    need a break today… due to a massive applescript overdose - am a applescript-beginner

… this is what i assembled so far.
it has all the basics i was looking for;

thanks a lot, again !


– ---------------------------------------------------------------------------------------------- CHANGING AN ALIAS’s TARGET


– … changes/re-links an existing alias to an existing file

– ---------------------------------------------------------------------------------------------- PREFS

– (at least) 2 existing files … :

– (at least) 1 ALIAS FILE … on Desktop:folderA:folderB:folderC
– Alias’ and folders’ names do not matter - works with any name
– file(s) need(s) to be within third folder though - won’t work otherwise

– 1 TARGET FILE … “testx.txt” on Desktop
– (target file’s name and location on desktop does matter - won’t work otherwise)


– ---------------------------------------------------------------------------------------------- step 0 — … select Alias file(s) in finder’s frontmost window

tell application “Finder”

set theSelection to selection – get and check the selected files in the frontmost finder window
repeat with aFile in theSelection – loop through the selected files
if class of aFile is alias file then – choose and proceed with all alias files


– ------------------------------------------------------------------------------ step 1 — … address existing selected source file/folder alias
– create variable existing source path of alias

set theAlias to aFile as text – the file/folder’s applescript formatted path

– check via … get theAlias
→ e.g. “Ohne Titel:Users:my_mac:Desktop:fix aliases:sample:Btest.txt alias”


– ------------------------------------------------------------------------------ step 2 — … address existing target file/folder’s path (without target name)
– create variable existing target path of alias

– --------------------------------------------------------------
– stepping up the file’s / folders’ hierachy
– --------------------------------------------------------------

– ---------------------- up three steps within parent directory

set theAliasPreA to container of aFile – path of 1. hierachie level
–( … whether “container” or “folder” … )
→ can be e.g. “Ohne Titel:Users:my_mac:Desktop:fix aliases:sample:”

set theAliasPreB to container of theAliasPreA – path of 2. hierachie level
→ can be e.g. “Ohne Titel:Users:my_mac:Desktop:fix aliases:”

set theAliasTrgt to container of theAliasPreB as text – path of 3. hierachie level
→ can be e.g.“Ohne Titel:Users:my_mac:Desktop:”


– ------------------------------------------------------------------------------ step 3 — … assemble existing target file/folder’s path (incl. target name)

set newAliasPath to theAliasTrgt & “testx.txt”

– check via … get newAliasPath
→ must be “Ohne Titel:Users:my_mac:Desktop:testx.txt”


– ------------------------------------------------------------------------------ step 4 — … change/apply variable target path (change current alias’ target)

set original item of file theAlias to file newAliasPath

end if
end repeat
end tell

this is a (little more refined) version which solves my problem.

… changes/re-links an existing alias to an existing file
… replaces a text-chunk of an alias’ “original item” property

--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- ---------------------------------------------------------------------------------------------- CHANGING AN ALIAS's TARGET (B)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
--
-- ...  changes/re-links an existing alias to an existing file
-- ... replaces text-chunk of an alias' "original item" property
--
-- --------------------------------------------------------------------------------------------------- PREFS
--
-- (at least) 2 existing files ... :
--
--
-- (at least) 1 ALIAS FILE:
-- ... Alias' and folders' names do not matter - works with any name in any location
-- ... Alias needs to target a file located in ... "fix aliases:sample:testx.txt" directory - this example won't work otherwise
--
--  e.g. ... Desktop:fix aliases:sample:"testx.txt alias"   
--
--
-- 1 TARGET FILE 
--  ... target file's name and location on desktop do matter -  this example won't work otherwise
--
-- ... Desktop:fix aliases:sample:folderA:"testx.txt"
--
--
--
-- ------------------------------------------------------------------------------------------------------- step 0a --- SET VALUES TO CHANGE
-- ---------------------------------------------------------------------   texts for replacements  
--
-- ------------------------------- will change target file's location within alias' "original item" property
--
--  from ... fix aliases:sample:testx.txt   ... to...     fix aliases:sample:folderA:testx.txt
--      
set WrdChckPre to "fix aliases:sample:testx.txt" --                    if  target path of alias contains this path element then ...
-- ... can be any part of the existing/current target path
-- ... does not need to contain target file's current name
--> for ... if OldTrgt contains  ... (see code below )
-- 
--      
set WrdLookUpPre to "fix aliases:sample:testx.txt" --               ... this target path chunk will be replaced with ...
-- ... does not need to contain target file's current name
--> for ... set WrdLookUp to ... (see code below )
--
--      
set WrdChngPre to "fix aliases:sample:folderA:testx.txt" --      ... this chunk
-- ... does not need to contain target file's name
--> for ... set WrdChng to ... (see code below )
--
--
-- ......... ! IF YOU INTEND TO CHANGE THE ALIAS' TARGET ONLY:
-- ......... NO NEED TO CHANGE PARAMETERS BELOW THIS LINE !
--
--
-- ... or, alternatively, e.g. ...
--
-- ------------------------------- change target file's location 
--
--  from ... fix aliases:sample:folderA:    ...to...     fix aliases:sample:
--
--     set WrdChckPre to "Desktop:fix aliases:sample:folderA:" --  if  target path of alias contains this path element then ...
--> for ... if OldTrgt contains "Desktop:fix aliases:" then
-- 
--     set WrdLookUpPre to "fix aliases:sample:folderA:" -- ... this target path chunk will be replaced with ...
--> for ... set WrdLookUp to "fix aliases:"
--
--     set WrdChngPre to "fix aliases:sample:" -- ... this chunk
--> for ... set WrdChng to "***"
--
--
--
-- --------------------------------------------------------------------------------------------------- step 0b --- ... select Alias in finder's frontmost window
--
tell application "Finder"
	--
	set theSelection to selection -- get and check the selected files in the frontmost finder window 
	--
	-- ---------------------------------------------------------------------------------------------- step 0c --- ... start loop
	--	
	repeat with aFile in theSelection -- loop through the selected files
		if class of aFile is alias file then -- choose and proceed with all aliases 
			--
			--
			-- ----------------------------------------------------------------------------------- step 1 --- ... address existing selected source file/folder alias
			-- create variable existing source path of alias  
			--            
			set theAlias to aFile as text -- the file/folder's applescript formatted path
			--
			--  check via ...  get theAlias
			--> e.g. "Ohne Titel:Users:my_mac:Desktop:fix aliases:sample:Btest.txt alias"
			--
			-- 
			set OldTrgt to original item of file theAlias as text -- the alias' current target
			--
			--
			-- ---------------------------------------------------------------------------------- step 2 ---  ... check/change existing target file/folder's path (without target name)
			-- check/change variable existing target path of alias	
			--
			--------------------------------------------------------------------------------------------------------------------------------------------------------------------
			-- ---------------------------------------------------- FIND & REPLACE TEXT IN A TEXTSTRING
			--------------------------------------------------------------------------------------------------------------------------------------------------------------------
			--
			--
			--------------------------------------------------------------------------------------------------------------  
			--------------------------------------------------------------------------------------------------------------  step 2a default/startup values
			--
			--
			-- ---------------------------------------------------------------------   default values (/reset from previous) 
			--
			-- 
			set WrdLookUp to "" -- reset the word to search for
			--
			set WrdChng to "" -- reset the replacement for the word
			--
			set TxtSrch to OldTrgt -- the text to check = string for aliases' current target path
			--> e.g. "Ohne Titel:Users:my_mac:Desktop:fix aliases:"
			--> e.g. "Ohne Titel:Users:my_mac:Desktop:"
			--
			set newAliasPath to OldTrgt ---- sets string for aliases' target path to current target path
			--> e.g. "Ohne Titel:Users:my_mac:Desktop"
			--
			--
			set theAliasTrgt to OldTrgt -- default path of  target file = current target file from selected alias
			-- OldTrgt is ... original item of file theAlias as text -- the alias' current target
			--
			-- ... or ..
			-- the following will change the result with every runthrough of this script
			-- set theAliasTrgt to "Ohne Titel:Users:my_mac:Desktop:fix aliases:sample:testx.txt" -- predefined path of  target file
			--> always resets target of alias to predefined target path
			-- ... 
			--
			--
			set RunChng to "stop" -- index that initiates or jumps functions for exchange of paths 
			-- "stop" ... jumps functions for exchange of paths
			-- "go" ... calls functions for / initiates change of pathnames
			--
			--
			--------------------------------------------------------------------------------------------------------------  
			--------------------------------------------------------------------------------------------------------------  step 2b check text for items to replace
			--
			--
			-- ---------------------------------------------------------------------   check if alias' path contains specific text chunk  
			--
			if OldTrgt contains WrdChckPre then --  if current target of alias contains specific path element
				--> e.g. ... if OldTrgt contains "fix aliases:sample:testx.txt" then 
				-- 
				set WrdLookUp to WrdLookUpPre -- the text to search for
				--> e.g. ... set WrdLookUp to "fix aliases:sample:testx.txt"
				--
				set WrdChng to WrdChngPre -- the word's replacement
				--> e.g. ... set WrdChng to "fix aliases:sample:folderA:testx.txt"
				-- 
				--
				set RunChng to "go" -- index that initiates change of pathnames by calling "if" clause below 
				--
			end if
			--
			--------------------------------------------------------------------------------------------------------------  
			--------------------------------------------------------------------------------------------------------------  step 2c execute replacement .... exchange parameters ... call replaceText handler
			--
			--
			-- -------------------------------------------------------------------------------------------------------------------------- step 2c.1 check if change was called/is neccessary
			-- 
			if RunChng contains "go" then -- index that initiates exchange of paths 
				-- ... "go" indicates do proceed/change path
				-- ... "stop" indicates do NOT proceed/change path and jumps this script
				-- 
				-- ... tests ...
				-- return WrdChng -- "fix aliases:sample:folderA:testx.txt"
				-- return WrdLookUp -- "fix aliases:sample:testx.txt"
				--
				-- -------------------------------------------------------------------------------------------------------------------- step 2c.2 save item delimiters for later use	
				--
				set prevTIDs to text item delimiters of AppleScript -- save/variablise applescript itemdelimiters
				--
				-- -------------------------------------------------------------------------------------------------------------------- step 2c.3 separate text at word-to-check
				--	
				set text item delimiters of AppleScript to WrdLookUp -- separate text whereever the word to check appears
				-- set text item delimiters of AppleScript to find -- separate text whereever the word to check appears
				--> e.g. ... set WrdLookUp to "fix aliases:sample:testx.txt"
				--
				--
				-- -------------------------------------------------------------------------------------------------------------------- step 2c.4 remove word-to-check from the text-to-check
				--
				set subject to text items of TxtSrch -- re-connect separate textelements
				-- variablises variable TxtSrch's text items
				-- ... TxtSrch is OldTrgt -- the text to check = string for aliases' current target path
				--> e.g. ... fix aliases:sample:testx.txt
				--
				-- ...or (for the sample's alternative as mentioned above)
				--> e.g. ... fix aliases:sample:folderA:testx.txt
				--
				--
				-- -------------------------------------------------------------------------------------------------------------------- step 2c.5 add replacement for the word-to-check
				--	
				set text item delimiters of AppleScript to WrdChng -- set itemdelimiter to content of variable "replace"
				-- set text item delimiters of AppleScript to replace -- set itemdelimiter to content of variable "replace" 
				--
				set subject to "" & subject -- sets back previous content of index subject and updates it
				--
				--
				set text item delimiters of AppleScript to prevTIDs -- resets applescript itemdelimiters
				--
				-- --------------------------------------------
				--
				set theAliasTrgt to subject ---- sets string for aliases' new target path for selected alias	
				--
				-- ... tests ...
				-- return theAliasTrgt --"Ohne Titel:Users:my_mac:Desktop:fix aliases:sample:folderA:testx.txt"
				--
			end if
			--
			--
			--------------------------------------------------------------------------------------------------------------------------------------------------------------------
			-- ---------------------------------------------------- END FIND & REPLACE TEXT IN A TEXTSTRING
			--------------------------------------------------------------------------------------------------------------------------------------------------------------------
			--
			--
			-- -------------------------------------------------------------------------------------------------------------------- step 3 ---  ... set existing target file/folder's  path (incl. target name)
			--	
			set newAliasPath to theAliasTrgt -- sets new or existing default target path for selected alias
			-- 
			-- check via ... get newAliasPath
			--> e.g. Ohne Titel:Users:my_mac:Desktop:fix aliases:sample:testx.txt 
			--> e.g. Ohne Titel:Users:my_mac:Desktop:fix aliases:sample:folderA:testx.txt
			--
			-- ... tests ...
			-- return newAliasPath
			--
			-- -------------------------------------------------------------------------------------------------------------------- step 4 ---  ...  change/apply variable target path (change current alias' target)      
			--               	   	
			set original item of file theAlias to file newAliasPath
			--   
			-- ... tests ...
			-- return original item of file theAlias -- won't update without this "return"  command           
			--
			--			
		end if
		--
		--		
	end repeat
	--
	--	
end tell

In addition to the previous thread:

Applescript will not allow to replace the “original item” property of an alias
if the referenced file/folder doesn’t reside on the computer’s harddrive.


To access a broken alias’ “original item” prop.
you may find a solution at:

get the target file/folder (“original item” property) of a broken alias
–>MacScripter topic “alias - original item - missing value ?”
http://macscripter.net/viewtopic.php?id=34551


To exchange an existing alias with a new one
at exactly the same position with the same colour label and name
but new, customised “original item” property
you may find a solution testing the script as shown below;
it …
DELETES AN EXISTING ALIAS
+
REPLACES IT WITH A NEW ALIAS USING CUSTOM PARAMETERS

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- DELETE EXISTING ALIAS + REPLACE IT WITH NEW ALIAS USING CUSTOM PARAMETERS
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------ WHAT THIS SCRIPT DOES
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
--
--
--
-- COPIES an existing alias' position-within-a-folder property 						(variable "OrgPos" - 	s.a. step 1.c.1)
-- COPIES an existing alias' label (colour) property 								(variable "OrgCol" - 	s.a. step 1.c.2)
-- COPIES an existing alias' name property 									(variable "aliasNm" - 	s.a. step 1.c.3)
--
-- CREATES a new Alias													(variable "aliasFile" - 	s.a. step 2.a)
-- ASSIGNS the existing alias' position and label property						(					s.a. step 2.a.1 and step 2.a.2)
-- and a target path ("original item" property) for an existing file or folder			(					s.a. step 2.a)
--
-- DELETES the existing alias, sets the new alias to the name of the deleted alias	(					s.a. step 2.b, step 2.c)
--
--
--
--- this script needs ...	
--
-- ... an existing alias file:													(variable "OrigFile" - 	s.a. step 1.a)
--  e.g.  "Macintosh HD:Users:my_mac:Desktop:New Test Alias File alias"
--
-- ... an existing target file or folder for the alias:								(variable "theFile" - 	s.a. step 1.b)
--  e.g.  "Macintosh HD:Users:my_mac:Desktop:New Test Alias File alias"
--
--
--
--			
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------- THE SCRIPT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
--
--
tell application "Finder"
	--
	--
	--																																																step 1 --- PREFS - SET VALUES
	--
	--																															1.a --- set value of path for existing alias
	-- 
	set OrigFile to file ("/Users/my_mac/Desktop/New Test Alias File alias" as POSIX file)
	--
	--																															1.b --- set value of path for target file of the new alias
	set theFile to "/Users/my_mac/Desktop/alias - create alias/scripts/" -- the link to the referenced file
	set theFile to theFile as POSIX file -- path as applescript formatted chunk						
	--
	-- e.g. ... file "Macintosh HD:Users:my_mac:Desktop:alias - create alias:scripts"
	--																					 										1.c --- get specs from existing alias --- the existing alias' properties
	--
	--																								1.c.1 --- position of alias	
	--
	set OrgPos to position of file (OrigFile as text) -- stores position of original alias--- displays when closing and opening again						
	--
	--  e.g. ... set position of file (aFile as text) to {86, 220} 
	-- 
	--																								1.c.2 --- label (colour) of alias	
	--
	set OrgCol to label index of file (OrigFile as text) -- stores  label (colour) of original alias								
	--							
	--  e.g. ... set OrgCol to 6				
	--
	--																								1.c.3 --- set name for new alias 	 
	set aliasNm to name of OrigFile -- creates name of alias
	--
	-- e.g. ... set aliasNm to "New Test Alias File alias"
	--
	--																															1.d --- get path of new folder for new alias --- the existing alias' folder's path
	--
	set alias_folder to container of OrigFile
	-- 		
	-- ... e.g. ... 	set alias_folder to item 1 of {folder "Desktop" of folder "my_mac" of folder "Users" of startup disk of application "Finder"}
	--
	--
	--																																																step 2 ... execute scripts
	--  * ... optional ... --> try -- keeps script from stopping/displaing message	
	--
	--																															step 2.a -- create new alias
	--
	--
	set aliasFile to make alias to theFile at alias_folder
	-- 
	-- e.g.  ... set aliasFile to make alias to theFile at (alias_folder as POSIX file)
	--
	--  ... or use e.g. ... set aliasFile to make new alias file at desktop to theFile -- creates alias on desktop
	--
	--																								2.a.1 --- assign position (within window)  to new alias	
	--  				
	set position of file (aliasFile as text) to OrgPos
	-- evaluates position of original alias
	--- displays correct positioning when closing window and opening again
	-- ... might not display position right after running the script ...
	--  e.g. ... get position of file (aFile as text) -- gets position
	--> {86, 220}	
	--																								2.a.2 --- assign label (-colour)  to new alias		
	--	
	set label index of file (aliasFile as text) to OrgCol
	-- evaluates  label (colour) of original alias
	-- e.g. ... get label index of file (aFile as text) -- gets label (colour)
	--> 6		
	--	
	--																															step 2.b -- delete existing alias 
	--
	set AliPth to (alias_folder as text) & aliasNm -- applescript formatted	
	--
	-- e.g. ... "Macintosh HD:Users:my_mac:Desktop:New Test Alias File alias"	
	--
	-- 						
	delete AliPth -- deletes existing alias if it has the same name as the new alias
	--
	-- e.g. ...delete "Macintosh HD:Users:my_mac:Desktop:New Test Alias File alias"
	--
	--	
	--																															step 2.c -- assign name to new alias		
	set the name of aliasFile to aliasNm -- creates name of alias	
	--
	-- e.g. ...set the name of aliasFile to "New Alias File" 
	--
	--  
	--  * ... optional ... --> end try -- end prevent script from stopping/displaing message
	--	
end tell
--