Thursday, August 17, 2017

#1 2017-07-25 04:13:45 am

msh
Member
Registered: 2014-01-25
Posts: 107

repair broken Finder aliases

I want to check my system for broken Finder aliases. I can get `original item` of an AppleScript alias and check if that `exists` but I cannot get it if it's broken, at least not with AppleScript, see https://github.com/rptb1/aliasPath. Can anyone confirm this? Could that code be bound into an applescript?
Here's the script I used. I haven't used AS in a while so feel free to amend it.

Applescript:


set l to do shell script "mdfind \"kMDItemKind == 'Alias'\""
set y to ""
set n to ""

repeat with e in paragraphs of l
   log e
   set p to POSIX file e
   log p
   try
       tell application "Finder"
           set a to p as alias
           if original item of a exists then
               set y to y & e & linefeed
           else
               set n to n & e & linefeed
               log "***try"
           end if
       end tell
   on error
       set n to n & e & linefeed
       log "***error"
   end try
end repeat

set f to open for access (path to desktop as text) & "FinderBrokenAliases" with write permission
write n as text to f
close access f


Filed under: Finder, finder alias

Offline

 

#2 2017-07-25 05:34:45 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3182

Re: repair broken Finder aliases

Getting the path of the original is not really difficult.

Applescript:

(* [url]http://www.macscripter.net/viewtopic.php?id=45852[/url] *)


set brokenAliasPOSIXPath to "/Volumes/Macintosh HD/Users/Important/Téléchargements/ achetés/maj_Apple Leopard"
set aFile to POSIX file brokenAliasPOSIXPath
set aFile to (aFile as text) as alias

tell application "Finder"
   set theWindows to every window
   repeat with awindow in theWindows
       # Close every info window
       if class of awindow is information window then close awindow
   end repeat
   select aFile
   
   repeat 10 times
       if ((get selection) as alias) = aFile then exit repeat # Wait for the window containing the file
       delay 0.2
   end repeat
end tell

tell application "System Events" to tell process "Finder"
   set frontmost to true
   keystroke "i" using {command down} # Ask for Info window
   tell application "Finder"
       repeat 10 times # Wait for the new info window
           delay 0.2
           if class of window 1 is information window then exit repeat
       end repeat
   end tell
   tell window 1
       --class of UI elements --> {scroll area, button, button, button, image, static text}
       tell scroll area 1
           --class of UI elements --> {image, static text, static text, static text, static text, scroll area, UI element, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, button, checkbox, UI element, static text, static text, UI element, static text, text field, checkbox, UI element, static text, UI element, static text, UI element, static text, static text, scroll area, button, button, menu button, checkbox}
           set theTexts to value of static texts
           repeat with i from 1 to count theTexts
               if theTexts's item i = "Original :" then
                   set theOriginal to theTexts's item (i + 1)
                   exit repeat
               end if
           end repeat
       end tell
       -- subrole of buttons --> {"AXCloseButton", "AXZoomButton", "AXMinimizeButton"}
       click button 1 # Close the Infos window
   end tell # window 1
end tell
theOriginal --> "/Important/Téléchargements/ téléchargés pour Leopard/maj_Apple"

CAUTION: Macintosh HD isn't my boot volume.

Edited the test checking for availability of the Info window whose title is a localized string.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mardi 25 juillet 2017 12:34:10

Last edited by Yvan Koenig (2017-07-25 07:28:43 am)

Offline

 

#3 2017-07-25 07:09:19 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11473
Website

Re: repair broken Finder aliases

This is a fast AppleScriptObjC version, basically it uses a similar way as the linked GitHub project

Applescript:


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

use framework "Foundation"

set allAliasFiles to do shell script "mdfind \"kMDItemKind == 'Alias'\""
set brokenAliasPaths to {}
repeat with anAlias in (get paragraphs of allAliasFiles)
   set aliasURL to (current application's NSURL's fileURLWithPath:anAlias)
   set {originalFile, theError} to (current application's NSURL's URLByResolvingAliasFileAtURL:aliasURL options:0 |error|:(reference))
   if theError is not missing value and theError's code() is 4 then
       set end of brokenAliasPaths to contents of anAlias
   end if
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set brokenAliases to brokenAliasPaths as text
set text item delimiters to TID

set fileDescriptor to open for access ((path to desktop as text) & "FinderBrokenAliases.txt") with write permission
write brokenAliases as text to fileDescriptor
close access fileDescriptor

Last edited by StefanK (2017-07-25 07:09:48 am)


regards

Stefan

Offline

 

#4 2017-07-25 07:12:51 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5079

Re: repair broken Finder aliases

Yvan Koenig wrote:

Getting the path of the original is not really difficult.



For some reason your script is returning this here:

error "The variable theOriginal is not defined." number -2753 from "theOriginal"


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2017-07-25 07:24:14 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5079

Re: repair broken Finder aliases

Stefan's solution is ideal, but if you need to support 10.9 and are happy to use a script library, this handler uses the same technique as the project referenced by the original poster:

Applescript:

use AppleScript version "2.3" -- 10.9 or later (must be in script lib for 10.9)
use framework "Foundation"
use scripting additions

my pathToOriginalFor:(POSIX path of (choose file))

on pathToOriginalFor:posixPath
   -- make URL and check it is an alias
   set fileURL to current application's |NSURL|'s fileURLWithPath:posixPath
   set {theResult, isAlias, theError} to fileURL's getResourceValue:(reference) forKey:(current application's NSURLIsAliasFileKey) |error|:(reference)
   if isAlias as boolean then
       -- it's an alias; get bookmark data
       set {theData, theError} to current application's |NSURL|'s bookmarkDataWithContentsOfURL:fileURL |error|:(reference)
       if theData = missing value then error theError's localizedDescription() as text
       -- get path from bookmark data
       set theValues to current application's |NSURL|'s resourceValuesForKeys:{current application's NSURLPathKey} fromBookmarkData:theData
       set originalPath to theValues's objectForKey:(current application's NSURLPathKey)
       return originalPath as text
   else
       -- not an alias; return original path
       return posixPath
   end if
end pathToOriginalFor:

Stefan's code uses a newer, simpler method.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#6 2017-07-25 07:28:49 am

msh
Member
Registered: 2014-01-25
Posts: 107

Re: repair broken Finder aliases

@Yvan Koenig, thanks but for such type of operations, particularly repetitive, I rather avoid UI scripting — and I don't think that would work with hidden files in Finder.

@StefanK, thanks, any way to make that binding work on 10.8?

Offline

 

#7 2017-07-25 07:35:53 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3182

Re: repair broken Finder aliases

Shane Stanley wrote:
Yvan Koenig wrote:

Getting the path of the original is not really difficult.



For some reason your script is returning this here:

error "The variable theOriginal is not defined." number -2753 from "theOriginal"



May you retry with the edited version available in message #2.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mardi 25 juillet 2017 14:35:47

Offline

 

#8 2017-07-25 07:42:03 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5079

Re: repair broken Finder aliases

Yvan Koenig wrote:

May you retry with the edited version available in message #2.



I see the problem: in French it presumably says "Original :", but here it uses English punctuation style, meaning no space before the colon: "Original:".


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#9 2017-07-25 07:45:58 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3182

Re: repair broken Finder aliases

msh wrote:

@Yvan Koenig, thanks but for such type of operations, particularly repetitive, I rather avoid UI scripting — and I don't think that would work with hidden files in Finder.

@StefanK, thanks, any way to make that binding work on 10.8?




On my machine "hidden files" are always visible so I never faced this problem
No hope to use ASObjC code on 10.8.

My own question :

When I run StefanK's script I get the list of the broken aliases, not the list of original files which is - to my understanding - what you want to get.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mardi 25 juillet 2017 14:45:54

Offline

 

#10 2017-07-25 08:12:29 am

msh
Member
Registered: 2014-01-25
Posts: 107

Re: repair broken Finder aliases

Yvan Koenig wrote:


No hope to use ASObjC code on 10.8.



Any other workaround with plain AS, besides UI scripting?

Yvan Koenig wrote:


When I run StefanK's script I get the list of the broken aliases, not the list of original files which is - to my understanding - what you want to get.



I already get the broken aliases with my script. I'd like to get both.

Offline

 

#11 2017-07-25 08:24:04 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11473
Website

Re: repair broken Finder aliases

If an alias file is broken, there is no original file.


regards

Stefan

Offline

 

#12 2017-07-25 09:16:15 am

msh
Member
Registered: 2014-01-25
Posts: 107

Re: repair broken Finder aliases

StefanK wrote:

If an alias file is broken, there is no original file.



There is the path to the broken Finder alias and the original location as seen in Get Info pane of that same alias. The code Iinked to in #1 retrieves the latter.

Offline

 

#13 2017-07-25 11:20:45 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11473
Website

Re: repair broken Finder aliases

I understand.

I compiled the code in the linked project and added the -b switch to print only the broken alias files along with the original paths.

You can download it here: AliasPath.zip

The binary is code-signed and the development target is 10.8

Save the binary somewhere, replace /Full/Path/To/AliasPath in the script with the real full POSIX path to the binary - if the path contains space characters wrap the full path in single quotes – and use it:

Applescript:

set allAliasFiles to paragraphs of (do shell script "mdfind \"kMDItemKind == 'Alias'\"")
set {TID, text item delimiters} to {text item delimiters, "' '"}
set aliasFilePaths to "'" & (allAliasFiles as text) & "'"
set text item delimiters to TID

set brokenAliases to do shell script "/Full/Path/To/AliasPath -b " & aliasFilePaths
set fileDescriptor to open for access ((path to desktop as text) & "FinderBrokenAliases.txt") with write permission
write brokenAliases to fileDescriptor
close access fileDescriptor

Last edited by StefanK (2017-07-25 11:40:09 am)


regards

Stefan

Offline

 

#14 2017-07-25 01:52:26 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2683
Website

Re: repair broken Finder aliases

msh wrote:
StefanK wrote:

If an alias file is broken, there is no original file.



There is the path to the broken Finder alias and the original location as seen in Get Info pane of that same alias. The code Iinked to in #1 retrieves the latter.



Keep in mind that StefanK has an point here. Maybe not useful for msh but for future viewers:

Alias is an "union" of symbolic link and hard link. Since Mac OS X 10.2 every time an alias needs to be resolved it will first try the symbolic link and then the hard link if the first one fails. However these alias data requires updates when an file is moved or renamed which doesn't happen in real time (when the alias is used for instance). With that in mind, the original file returned by the alias doesn't have to be the latest location of the file. The "orginal file" can be any location where the file has ever been stored.

Why the get info pane shows original location is unclear to me. Just move, rename and remove the file and the info pane will show wrong location.

Offline

 

#15 2017-07-25 04:40:42 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3182

Re: repair broken Finder aliases

Hi Stefan

To get your script running flawlessly I converted an objet in quoted form

Applescript:

set brokenAliases to do shell script "/Users/admin/bin/AliasPath -b " & quoted form of aliasFilePaths

When I ran it I discovered that it is useless.
It returns a list of the path of the broken aliases, no less, no more.
In fact I'm not surprised because I get the same behavior when I try to use ASObjC to get the original of broken aliases.

So I decided to re-work my proposal. At this time, it is :

Applescript:

tell application "Finder"
   set theWindows to every window
   repeat with awindow in theWindows
       # Close every info window once so we will not have to do it in the handler
       if class of awindow is information window then close awindow
   end repeat
end tell

set l to do shell script "mdfind \"kMDItemKind == 'Alias'\""
set y to ""
set n to ""
set pass to 0
repeat with e in paragraphs of l
   set pass to pass + 1
   log e
   set p to POSIX file e
   log p
   try
       tell application "Finder"
           set a to p as alias
           if original item of a exists then
               set theOriginal to my getOriginal(e) # here e is a POSIX path
               set y to y & e & tab & theOriginal & linefeed # Add path2alias & path2original in y
           else
               set theOriginal to my getOriginal(e) # here e is a POSIX path
               set n to n & e & tab & theOriginal & linefeed # Add path2alias & path2original in n
               log "***try"
           end if
       end tell
   on error
       set n to n & e & tab & "***error" & linefeed # Here, I get that with aliases appearing as greyed in the Finder window
       log "***error"
   end try
   --if pass = 50 then exit repeat # active only during tests
end repeat

# Save the list of broken aliases
set f to open for access (path to desktop as text) & "FinderBrokenAliasesXXX.txt" with write permission
write n as text to f
close access f
# Save the list of correct aliases
set f to open for access (path to desktop as text) & "FinderCorrectAliasesXXX.txt" with write permission
write y as text to f
close access f

on getOriginal(brokenAliasPOSIXPath)
   set aFile to POSIX file brokenAliasPOSIXPath
   
   tell application "Finder"
       (*
       set theWindows to every window
       repeat with awindow in theWindows
           # Close every info window
           if class of awindow is information window then close awindow
       end repeat
       *)

       select aFile # open the window of the folder containing the alias
       
       set aFile to aFile as text
       
       repeat 10 times
           if ((get selection) as text) = aFile then exit repeat # Wait for the window containing the file
           delay 0.2
       end repeat
   end tell
   
   tell application "System Events" to tell process "Finder"
       set frontmost to true
       keystroke "i" using {command down} # Ask for Info window
       tell application "Finder"
           repeat 10 times # Wait for the new info window
               delay 0.2
               if class of window 1 is information window then exit repeat
           end repeat
       end tell
       tell window 1
           --class of UI elements --> {scroll area, button, button, button, image, static text}
           tell scroll area 1
               --class of UI elements --> {image, static text, static text, static text, static text, scroll area, UI element, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, button, checkbox, UI element, static text, static text, UI element, static text, text field, checkbox, UI element, static text, UI element, static text, UI element, static text, static text, scroll area, button, button, menu button, checkbox}
               set theTexts to value of static texts
               repeat with i from 1 to count theTexts
                   if theTexts's item i is in {"Original :", "Original:"} then
                       set theOriginal to theTexts's item (i + 1)
                       exit repeat
                   end if
               end repeat
           end tell
           -- subrole of buttons --> {"AXCloseButton", "AXZoomButton", "AXMinimizeButton"}
           click button 1 # Close the Infos window
           if i = (count theTexts) then set theOriginal to "?¿?¿?¿?¿?¿" # The window doesn't display an original, build a fake string
       end tell # window 1
       tell application "Finder" to close window 1
   end tell
   return theOriginal --> "/Important/Téléchargements/ téléchargés pour Leopard/maj_Apple"
end getOriginal

I'm not satisfied because :
(1) at this time it works only with French and English localization
(2) I'm not satisfied by the treatment of aliases generation the message "***error"
Here, these aliases are greyed in their Finder window and it's that which generate the error. It's boring because when I open their Info window by hand, I see that their original is defined.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mardi 25 juillet 2017 23:40:21

Offline

 

#16 2017-07-25 05:11:37 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11473
Website

Re: repair broken Finder aliases

Yvan,

the CLI is supposed to return the path of the alias file and the original separated by `: ` in one line for each found path and the arguments are supposed to be passed in format 'path1' 'path2' 'path3' each in single quotes and separated by a space (the text item delimiter is single quote + space + single quote).

Last edited by StefanK (2017-07-25 05:13:35 pm)


regards

Stefan

Offline

 

#17 2017-07-25 06:50:04 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5079

Re: repair broken Finder aliases

DJ Bazzie Wazzie wrote:

Since Mac OS X 10.2 every time an alias needs to be resolved it will first try the symbolic link and then the hard link if the first one fails.



Mostly. Apple has said the method used in alias resolution is opaque and subject to change, and I'm pretty sure it was the other way around at one stage.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#18 2017-07-25 07:37:18 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2683
Website

Re: repair broken Finder aliases

Shane Stanley wrote:
DJ Bazzie Wazzie wrote:

Since Mac OS X 10.2 every time an alias needs to be resolved it will first try the symbolic link and then the hard link if the first one fails.



…and I'm pretty sure it was the other way around at one stage.



Correct, before Mac OS X 10.2 it was the other way around.

Offline

 

#19 2017-07-26 04:19:38 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3182

Re: repair broken Finder aliases

StefanK wrote:

Yvan,

the CLI is supposed to return the path of the alias file and the original separated by `: ` in one line for each found path and the arguments are supposed to be passed in format 'path1' 'path2' 'path3' each in single quotes and separated by a space (the text item delimiter is single quote + space + single quote).




It seems that "supposed" is the important word.

If I run the script with the syntax proposed in your message:

Applescript:

set allAliasFiles to paragraphs of (do shell script "mdfind \"kMDItemKind == 'Alias'\"")
set {TID, text item delimiters} to {text item delimiters, "' '"}
set aliasFilePaths to "'" & (allAliasFiles as text) & "'"
set text item delimiters to TID

set brokenAliases to do shell script "/Users/admin/bin/AliasPath -b " & aliasFilePaths
set fileDescriptor to open for access ((path to desktop as text) & "FinderBrokenAliasesLast.txt") with write permission
write brokenAliases to fileDescriptor
close access fileDescriptor

I get the error :
error "sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `/Users/admin/bin/AliasPath -b '/Users/??????????/Library/Speech/Speakable Items/Application Speakable Items/Safari/Target Application Alias' '/Library/Fonts/Monospaced ƒ/Andale Mono.ttf' '/Library/Fonts/disabled ƒ/Cursives/SchoolHouse Cursive B.dfont' '/Library/Fonts/disabled ƒ/Cursives/Santa Fe LET Fonts.dfont' '/Applications/Adobe Acrobat DC/Acrobat Uninstaller' '/Users/??????????/Downloads/Developer/Tools' '/Users/??????????/Downloads/Developer/Documentation/DocSets' '/Users/??????????/Downloads/Developer/Library/Xcode/Xcode' '/Users/??????????/Downloads/Developer/Library/Xcode/PrivateFrameworks' '/Users/??????????/Downloads/Developer/Library/Xcode/Frameworks' '/Users/??????????/Downloads/Developer/Platforms/MacOSX.platform' …


I'm not too surprising by the issued error because as you may  see, the path to broken aliases may contain space or "ƒ" characters and, but you can't see that, they may also contain characters like "…", "œ","é" and some other non ASCII ones.

Trying to get rid of that, I edited the shell instruction as :

Applescript:

set brokenAliases to do shell script "/Users/admin/bin/AliasPath -b " & quoted form of aliasFilePaths

Alas, I got :
path to desktop as text
        --> "SSD 500:Users:??????????:Desktop:"
    open for access "SSD 500:Users:??????????:Desktop:FinderBrokenAliasesLast.txt" with write permission
        --> 114
    write "" to 114
    close access 114

Yes, write nothing.
As the script doesn't set EOF to 0 before writing, it doesn't change an existing file.
This is why, yesterdays I retrieved the list of broken aliases in the file.
To get rid of that, I added one instruction:

Applescript:

set eof of fileDescriptor to 0

before the write one.

As I can't post a screenshot I added instructions checking the availability of the CLI in the named folder.
Here is the relevant part of the history:

Applescript:

/Volumes/Macintosh HD/Developer/Library/Frameworks/CPlusTest.framework
/Volumes/Macintosh HD/Developer/Library/PrivateFrameworks/IDEBundleInjection.framework"
end tell
tell application "
System Events"
   exists file "
/Users/admin/bin/AliasPath"
       --> true
   (*true*)
end tell
tell current application
   do shell script "
/Users/admin/bin/AliasPath -b ''\\''/Users/

As some paths may look surprising, I repeat that I don't boot from "Macintosh HD" but from an external SSD.
Macintosh HD (mechanical HD) contain a replica of the SSD used as a backup.
Booting from the SSD fasten everything.


Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mercredi 26 juillet 2017 11:19:25

Offline

 

#20 2017-07-26 06:34:17 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5079

Re: repair broken Finder aliases

Yvan,

I'm not sure of the point you are making, but perhaps you should try:

Applescript:

write brokenAliases to fileDescriptor as «class utf8»


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#21 2017-07-26 06:53:22 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2683
Website

Re: repair broken Finder aliases

The quoting of StefanK is tricky because it doesn't use quoted form so if a path contains a single quote it will break down.

Applescript:

set allAliasFiles to paragraphs of (do shell script "mdfind \"kMDItemKind == 'Alias'\"")
repeat with i from 1 to count allAliasFiles
   set item i of allAliasFiles to quoted form of item i of allAliasFiles
end repeat
tell AppleScript
   set oldTIDs to text item delimiters
   set text item delimiters to space
   set bashList to allAliasFiles as string
   set text item delimiters to oldTIDs
end tell

set brokenAliases to do shell script "/Users/admin/bin/AliasPath -b " & aliasFilePaths
set fileDescriptor to open for access ((path to desktop as text) & "FinderBrokenAliasesLast.txt") with write permission
write brokenAliases to fileDescriptor
close access fileDescriptor

Offline

 

#22 2017-07-26 07:37:13 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3182

Re: repair broken Finder aliases

Shane Stanley wrote:

Yvan,

I'm not sure of the point you are making, but perhaps you should try:

Applescript:

write brokenAliases to fileDescriptor as «class utf8»




Using a different encoding is not relevant here, it's the data to save which is an empty string ( "" )

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mercredi 26 juillet 2017 14:37:08

Offline

 

#23 2017-07-26 08:17:22 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3182

Re: repair broken Finder aliases

DJ Bazzie Wazzie wrote:

The quoting of StefanK is tricky because it doesn't use quoted form so if a path contains a single quote it will break down.

Applescript:

set allAliasFiles to paragraphs of (do shell script "mdfind \"kMDItemKind == 'Alias'\"")
repeat with i from 1 to count allAliasFiles
   set item i of allAliasFiles to quoted form of item i of allAliasFiles
end repeat
tell AppleScript
   set oldTIDs to text item delimiters
   set text item delimiters to space
   set bashList to allAliasFiles as string
   set text item delimiters to oldTIDs
end tell

set brokenAliases to do shell script "/Users/admin/bin/AliasPath -b " & aliasFilePaths
set fileDescriptor to open for access ((path to desktop as text) & "FinderBrokenAliasesLast.txt") with write permission
write brokenAliases to fileDescriptor
close access fileDescriptor




Hi DJ

At first I had to edit a bit your code because the variable aliasFilePaths was not defined.

So I edited the main instruction as :

Applescript:

set brokenAliases to do shell script "/Users/admin/bin/AliasPath -b " & bashList --allAliasFiles

Alas when I tested it I got :

--> error "/Users/??????????/Library/Speech/Speakable Items/Application Speakable Items/Safari/Target Application Alias: The file “Target Application Alias” couldn’t be opened because there is no such file.
2017-07-26 14:53:54.036 AliasPath[2136:2454891] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fffa90b157b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffbe30a1da objc_exception_throw + 48
    2   CoreFoundation                      0x00007fffa912ec55 +[NSException raise:format:] + 197
    3   Foundation                          0x00007fffaaa69e8a -[NSURL(NSURL) initFileURLWithPath:] + 131
    4   Foundation                          0x00007fffaaa69df1 +[NSURL(NSURL) fileURLWithPath:] + 45
    5   AliasPath                           0x0000000103a287cf main + 863
    6   libdyld.dylib                       0x00007fffbebeb235 start + 1
    7   ???                                 0x00000000000001b2 0x0 + 434
)
libc++abi.dylib: terminating with uncaught exception of type NSException" number 1006

I'm really surprised because when I run :

Applescript:

set POSIXPath to "/Users/??????????/Library/Speech/Speakable Items/Application Speakable Items/Safari/Target Application Alias"
set maybe to POSIX file POSIXPath

tell application "System Events"
   exists disk item POSIXPath
   exists maybe
end tell
tell application "Finder"
   exists maybe
end tell
set maybeAlias to (maybe as text) as alias

I get :

Applescript:


CAUTION this is not a script but an history
tell application "System Events"
   exists disk item "/Users/??????????/Library/Speech/Speakable Items/Application Speakable Items/Safari/Target Application Alias"
       --> false
   exists file "SSD 500:Users:??????????:Library:Speech:Speakable Items:Application Speakable Items:Safari:Target Application Alias"
       --> true
end tell
tell application "Finder"
   exists file "SSD 500:Users:??????????:Library:Speech:Speakable Items:Application Speakable Items:Safari:Target Application Alias"
       --> false
Résultat :
error "Le fichier SSD 500:Users:??????????:Library:Speech:Speakable Items:Application Speakable Items:Safari:Target Application Alias est introuvable." number -43 from "SSD 500:Users:??????????:Library:Speech:Speakable Items:Application Speakable Items:Safari:Target Application Alias"

Yes, you read well : the file doesn't exist when we test its POSIX path but it does when we test the POSIX file.
And there is no such alias.


Given that I'm not too puzzled by the returned error.
I guess that the case "the file doesn't exist" is not filtered in the CLI.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mercredi 26 juillet 2017 15:17:06

Offline

 

#24 2017-07-26 11:04:03 am

msh
Member
Registered: 2014-01-25
Posts: 107

Re: repair broken Finder aliases

Yvan Koenig wrote:

Yes, you read well : the file doesn't exist when we test its POSIX path but it does when we test the POSIX file.
And there is no such alias.



Has anyone else tested StefanK's AppleScriptObjC version?

I also run into that error, Yvan.

error "Scripting component error." number -1750 from file "1:Users:admin:Library:Speech:Speakable Items:Application Speakable Items:Internet Explorer:Target Application Alias" to alias



It seems there's a CR (^M) at the end of those files named 'Target Application Alias'. Move one of these to the shell and press tab to complete it.

Eventually I came up with this. First time I ran it in AppleScript Editor, I got the error above. The second time it ran successfully and I haven't gotten my head around that. If I run it from Quicksilver it does fine the first round, always.

Applescript:


-- [url]http://macscripter.net/viewtopic.php?id=45852[/url]
-- [url]https://github.com/rptb1/aliasPath[/url]

set l to do shell script "mdfind \"kMDItemKind == 'Alias'\""
set x to "/usr/local/bin/aliasPath"
set y to ""
set n to ""

repeat with e in paragraphs of l
   log e
   set p to POSIX file e
   log p
   try
       tell application "Finder"
           set a to p as alias
           if original item of a exists then
               set y to y & e & linefeed
           else
               --set n to n & e & linefeed
               set o to do shell script x & space & quoted form of e
               set n to n & e & linefeed & o & linefeed & linefeed
               log "***try"
           end if
       end tell
   on error
       --set n to n & e & linefeed
       set o to do shell script x & space & quoted form of e
       set n to n & e & linefeed & o & linefeed & linefeed
       log "***error"
   end try
end repeat

set f to open for access (path to desktop as text) & "Finder Broken Aliases" with write permission
write n as text to f
close access f

AFAIR, the only differences from the one I posted above are, set x and o and reset n.

I checked GetFileInfo (IIRC from dev tools) and fileinfo (from osxutils) but none get the original path like aliasPath does. I rather use a builtin instead of an additional binary, or, if I'd use a supported system, StefanK's AppleScriptObjC version. Oh well…

Last edited by msh (2017-07-26 11:34:14 am)

Offline

 

#25 2017-07-26 12:57:19 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3182

Re: repair broken Finder aliases

I think that understood where the "missing" aliases are coming from.

As I wrote, the mechanical HD is used as a backup of the data stored on the boot drive named "SSD500"
There is an alias named "Target Application Alias" in this mechanical HD.
On this device, the alias is stored as "/Users/??????????/Library/Speech/Speakable Items/Application Speakable Items/Safari/Target Application Alias"

But, as the script is executed from the device named "SSD 500", the system treat this path as one pointing to an item stored on "SSD 500", in Hfs format it is "SSD 500:Users:??????????:Library:Speech:Speakable Items:Application Speakable Items:Safari:Target Application Alias"
although it's real location is "Macintosh HD:Users:??????????:Library:Speech:Speakable Items:Application Speakable Items:Safari:Target Application Alias"

Worse, there is something resembling to what msh described: when I search for it with Spotlight, the filename appear as it there was a linefeed or a return at its very end. Curious, when I open the folder containing it, the name appears as if the linefeed or return was gone.

As I don't see how to re-attribute the path to the correct volume, I think that the problem would be solved by the introduction of instructions filtering the case "file doesn't exist" in the CLI.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mercredi 26 juillet 2017 19:57:13

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)