I’m rather new to AppleScript, so I could use any advice on what I’ve done incorrectly in my first attempt. The portion that extracts the original alias names works fine, but I can’t seem to get the logging file to open and record my entries.
Here is what I’ve cobbled together, so far, from code snippets …
-------------------------------------------------------------------------------------------
# PGM : Find and log info on broken aliases.
-------------------------------------------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property LogStatus : true
try
-- Have the User select the top folder of the heirarchy that is to be checked and repaired.
set My_Tree to choose folder with prompt "Select top folder:" without invisibles
-- Create an alias list of all alias files and alias folders in the selected heirarchy.
-- Note: This coerces the 'entire contents' directly, so there's no error getting 'every item' when there aren't any.
tell application "Finder"
try
set aliasList to alias files of entire contents of My_Tree as alias list
on error
set aliasList to {alias files of entire contents of My_Tree as alias}
end try
end tell
-- Examine each alias in the alias list.
repeat with theAliasFile in aliasList
set posixFilePath to POSIX path of theAliasFile
set theOriginalItem to (my nameOfOriginalFileForAlias:posixFilePath)
set theStatus to ("Original Name: " & (theOriginalItem as string))
display dialog (theStatus)
DoLog("Progress: ", theStatus)
end repeat
on error e
-- log the error
end try
-------------------------------------------------------------------------------------------
-- HANDLERS
-------------------------------------------------------------------------------------------
on nameOfOriginalFileForAlias:posixPath
tell current application's class "NSURL"
# Make URL
set anNSURL to its fileURLWithPath:posixPath
# Load data from alias file
set theData to its bookmarkDataWithContentsOfURL:anNSURL |error|:(missing value)
# Get value as dictionary from data
set theResult to its resourceValuesForKeys:{current application's NSURLPathKey} fromBookmarkData:theData
end tell
# Extract original item and coerce to text
return (theResult's objectForKey:(current application's NSURLPathKey)) as text
end nameOfOriginalFileForAlias:
----------------------------------------
my DoLog("File Error", "Not Closed")
on DoLog(TheName, TheComment)
if LogStatus is true then
set the LogFile to ((path to desktop) as text) & "Script.log"
try
open for access file the LogFile with write permission
set TimeStamp to do shell script "date -n '+%d/%m/%Y %H:%M:%S'"
write ((TheName as string) & tab & TimeStamp & tab & (TheComment as string) & return) to file the LogFile starting at eof as string
close access file the LogFile
return true
on error
try
close access file the LogFile
return false
end try
end try
end if
end DoLog
I’ve had another go at getting the script to work, but I still am having difficulties opening and writing to a file. Hope someone can help.
Here is what I have so far.
-------------------------------------------------------------------------------------------
# PGM : Find and log info on broken aliases.
-------------------------------------------------------------------------------------------
use AppleScript version "2.7" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property LogStatus : true
try
set this_file to (path to desktop as Unicode text) & "Log.txt"
-- Have the User select the top folder of the heirarchy that is to be checked and repaired.
set My_Tree to choose folder with prompt "Select top folder:" without invisibles
-- Create an alias list of all alias files and alias folders in the selected heirarchy.
-- Note: This coerces the 'entire contents' directly, so there's no error getting 'every item' when there aren't any.
tell application "Finder"
try
set aliasList to alias files of entire contents of My_Tree as alias list
on error
set aliasList to {alias files of entire contents of My_Tree as alias}
end try
end tell
-- Examine each alias in the alias list.
repeat with theAliasFile in aliasList
set posixFilePath to POSIX path of theAliasFile
set theOriginalItem to (my nameOfOriginalFileForAlias:posixFilePath)
set theStatus to ("Original Name: " & (theOriginalItem as string) & return)
display dialog (theStatus)
write_to_file(theStatus, this_file, true)
end repeat
on error error_message number error_number
set this_error to "Error: " & error_number & ". " & error_message & return
display dialog (this_error)
set log_file to (path to desktop as Unicode text) & "Script Error Log.txt"
my write_to_file(this_error, log_file, true)
end try
-------------------------------------------------------------------------------------------
-- HANDLERS
-------------------------------------------------------------------------------------------
on nameOfOriginalFileForAlias:posixPath
tell current application's class "NSURL"
# Make URL
set anNSURL to its fileURLWithPath:posixPath
# Load data from alias file
set theData to its bookmarkDataWithContentsOfURL:anNSURL |error|:(missing value)
# Get value as dictionary from data
set theResult to its resourceValuesForKeys:{current application's NSURLPathKey} fromBookmarkData:theData
end tell
# Extract original item and coerce to text
return (theResult's objectForKey:(current application's NSURLPathKey)) as text
end nameOfOriginalFileForAlias:
----------------------------------------
on write_to_file(this_data, target_file, append_data)
display dialog (this_data)
try
-- open for access target_file with write permission
set fRef to (open for access file target_file with write permission)
if append_data is false then set eof of target_file to 0
write (this_data as Unicode text) to fRef starting at eof
close access fRef
return true
on error
try
close access fRef
end try
return false
end try
end write_to_file