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).
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.
If I run the script with the syntax proposed in your message:
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 :
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:???”
open for access “SSD 500:Users:???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:
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:
/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
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.
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
At first I had to edit a bit your code because the variable aliasFilePaths was not defined.
So I edited the main instruction as :
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 :
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 :
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
Has anyone else tested StefanK’s AppleScriptObjC version?
I also run into that error, Yvan.
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.
-- http://macscripter.net/viewtopic.php?id=45852
-- https://github.com/rptb1/aliasPath
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…
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
set allAliasFiles to (do shell script "mdfind \"kMDItemKind == 'Alias'\"" without altering line endings)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {linefeed}
set allAliasFiles to text items of allAliasFiles
set AppleScript's text item delimiters to saveTID
Good idea Shane but it doesn’t solve the problem.
Yesterdays, when I faced the problem I ran this extract of the main script :
set allAliasFiles to (do shell script "mdfind \"kMDItemKind == 'Alias'\"")
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {linefeed}
set allAliasFiles to text items of allAliasFiles
set AppleScript's text item delimiters to saveTID
log (get count allAliasFiles)
allAliasFiles
and the returned count was 1.
New attempt trying to split upon return :
set allAliasFiles to (do shell script "mdfind \"kMDItemKind == 'Alias'\"")
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return}
set allAliasFiles to text items of allAliasFiles
set AppleScript's text item delimiters to saveTID
log (get count allAliasFiles)
allAliasFiles
This time, the count was 432 and the list contained :
Not surprising running your original script fails due to the character “'” available in some file names.
With the code modified by DJ,
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 -bo " & aliasFilePaths
set fileDescriptor to open for access ((path to desktop as text) & "FinderBrokenAliases.txt") with write permission
write brokenAliases to fileDescriptor
close access fileDescriptor
I got :
--> error "/Users/??????????/Library/Speech/Speakable Items/Application Speakable Items/System Preferences/Target Application Alias is not a valid path" number 1
in which the named path is the first of the listed aliases.
As one of the annoying aliases is:
/Users/???/Library/Speech/Speakable Items/Application Speakable Items/Safari/Target Application Alias
I guess that it may exist on your machine
An other one supposed to be available is :
/Users/???/Library/Speech/Speakable Items/Application Speakable Items/AppleScript Editor/Target Application Alias
Here, most of these “annoying” aliases are dated 2012/07/30, some are dated 2013/03/15.
So I don’t know if they are really useful at this time.
They exist for these applications :
AppleScript Editor
Finder
HexEdit
ImageWell
Internet Explorer
iTunes
JavaApplicationStub
Mail
Numbers
Pages
Safari
System Preferences
VLC
Xcode
If they don’t exist for you, I may send you one of them for testing.
Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) jeudi 27 juillet 2017 12:49:40
The explicit Spotlight search (the mdfind line) is not needed anymore in AliasPath1.1, the CLI performs the search on Cocoa level. Delete the first 4 lines and the parameter & aliasFilePaths in the next line.
And if your file names contain a lot of UTF 8 characters you have to write the text to disk UTF8 encoded.
write brokenAliases to fileDescriptor as «class utf8»
set brokenAliases to do shell script "/Users/admin/bin/AliasPath -bo"
set fileDescriptor to open for access ((path to desktop as text) & "FinderBrokenAliases.txt") with write permission
write brokenAliases to fileDescriptor
close access fileDescriptor
If you need extraneous details, you may ask directly thru my personal mailbox.
On my side I have a version doing the job but using GUIscripting. With 432 path returned by mdfind the entire process took 347 seconds.
set startTime to current date
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
*)
close every window
end tell
set l to paragraphs of (do shell script "mdfind \"kMDItemKind == 'Alias'\"")
set y to ""
set n to ""
set pass to 0
repeat with e in l
set pass to pass + 1
set p to POSIX file e
try
tell application "Finder"
try
set a to p as alias
on error
# Retry after adding return at end of the path
set e to e & return
tell me to set p to POSIX file e
set a to p as alias
end try
set theOriginal to my getOriginal(e) # here e is a POSIX path
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
set itsDuration to (current date) - startTime
tell application (path to frontmost application as string) to display dialog "It tooks : " & itsDuration & " seconds"
on getOriginal(brokenAliasPOSIXPath)
set aFile to POSIX file brokenAliasPOSIXPath
tell application "Finder"
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 # Finder
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 # Finder
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 starts with "/" then
set theOriginal to theTexts's item i
log "got the original : " & theOriginal
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 # System Events …
tell application "Finder" to close window 1
if i = (count theTexts) then set theOriginal to "** no original available **" # The window doesn't display an original, build a fake string
return theOriginal --> "/Important/Téléchargements/ téléchargés pour Leopard/maj_Apple"
end getOriginal
#=====
This time I am satisfied because it no longer rely upon a localized string and it treat the “annoying” pathnames (using brute force).
Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) jeudi 27 juillet 2017 13:58:44