C-Command Software Forum

Help with AppleScript to create a file alias

I’m trying to write an applescript that would take the selected records (in this case PDFs) in EagleFiler and create Finder aliases in another folder of the same EF library. The reason for this is because I need to create a new project folder in this library that will use some files located in a “Papers” folder in “Records” and instead of moving the files to the new project folder (avoiding that other things might break when the file disappears from the original location), I was thinking that a Finder alias to these files could work well (alternatively could also use symlinks – for this case it would be fine as I don’t expect the original file path to change – but was first trying with the aliases).

In the AppleScript I have now, I’m guessing the file alias is created correctly since I can see the alias in Finder, can open it and can also have a “Quick Look”. However, when I do a rescan in EF, so that the file/alias is imported, the file pane is grey, not showing anything (the alias is selected in the records pane), so it seems that something is not working properly. Interestingly, if I create an alias manually in Finder and then move it to the new folder, the contents of the file/alias is displayed properly in EF.

Does anyone have a hint on how this could be fixed, and wouldn’t mind to share? (the AppleScript code is below)

On a related note, is there a straightforward way to tell EF directly from AppleScript to import the new alias record, without having to manually rescan the library?

Many thanks,
Pedro

tell application "EagleFiler"
	set _records to selected records of browser window 1
	tell library document 1
		set libPath to file of root folder
		set theOutputFolder to choose folder with prompt ¬
			"Select the destination folder" default location libPath
		
		repeat with _record in _records
			set _file to _record's file
			tell application "Finder"
				make new alias file at theOutputFolder to _file
			end tell
			--import files {_file} container theOutputFolder (not working; just a reminder of a possible syntax)
		end repeat
	end tell
end tell

Another way to do something like this would be to tag the files in that folder.

When I do this EagleFiler’s records list shows the Kind of the record as Alias, and the viewer shows the icon with the arrow at the lower left corner. Is that what you mean?

I don’t see (or expect) any difference doing it this way, but perhaps I’m not understanding what you mean by “move it to the new folder.” Perhaps you are importing in a way that EagleFiler is following the alias and importing a copy of the original file instead of an alias?

Here’s an example of how to import a file that was created by Finder:

tell application "EagleFiler"
    set _records to selected records of browser window 1
    tell library document 1
        set libPath to file of root folder
        set theOutputFolder to choose folder with prompt ¬
            "Select the destination folder" default location libPath
        
        repeat with _record in _records
            set _file to _record's file
            tell application "Finder"
                set _finderAliasFile to make new alias file at theOutputFolder to _file
                set _aliasAlias to _finderAliasFile as alias
                set _aliasPath to _aliasAlias's POSIX path
            end tell
            set _aliasFile to _aliasPath as POSIX file
            -- This would work except that you can't import a file that's already in the library folder
            -- because of the location of theOutputFolder.
            -- import files {_aliasFile}
            
            -- So we scan instead so that EagleFiler notices the new file.
            scan for new files
        end repeat
    end tell
end tell

Thank you Michael!

So it seems that this isn’t working with me. I’m allowing duplicates in my library but when I run this script I get the same behaviour as before, a grey area in the record view pane. This is illustrated in the first screenshot.

I actually overlooked a bit this, because in Finder the alias was created in the same folder, which means that the name was not the same as the original file (it attaches a “alias” to the end of the filename).

If I rename the file within the AppleScript with a shell command:

set srcFile to quoted form of ((POSIX path of theOutputFolder as string) & _name)
set dstFile to quoted form of ((POSIX path of theOutputFolder as string) & _name & " alias")
do shell script "mv " & srcFile & " " & dstFile

then EagleFiler does display the Alias icon but the viewer actually shows the file contents…

Although this way does seem to be working, not sure if this is the expected behaviour from what you were describing.

Many thanks,
Pedro

Maybe there’s something going on with my system, because when I open a terminal window in the “Aliases” folder I can see the “Doe 1001a.pdf alias” file but not the one created with the first AppleScript:

$ ls -a
./                          .DS_Store                   Doe 1001a.pdf alias
../                         .EagleFiler Metadata.plist

I think the PDF viewer is showing this because the file doesn’t exist.

You could also create the alias with the proper name to begin with:

set _aliasName to _record's filename & " alias"
tell application "Finder"
    set _finderAliasFile to make new alias file at theOutputFolder to _file with properties {name:_aliasName}

That’s not what I expected (or what I see on my Mac with PDF aliases), but I’m glad to hear that it’s doing what you wanted. Perhaps I can make this an official feature in the future.

The solution I was trying, prompting for the user to choose a destination folder, is actually far from ideal, because it was quite difficult (at least for me…) to set the correct properties (title, tags, etc.) of the new alias file as in the source file. This could easily be achived with the import files command though, so I basically made use of EagleFiler’s “Duplicate Records” AppleScript and just made some modifications to create a Finder alias instead of a copy. The new alias records are created in the same container/folder as the selected records with an " alias" suffix, which can then be moved to the target folder within EagleFiler (e.g. using “Move To”).

I’m posting the AppleScript here if anyone finds this useful:

-- Largely adapted from EagleFiler's "Duplicate Records" applescript
tell application "EagleFiler"
	set _records to selected records of browser window 1
	tell library document 1
		repeat with _record in _records
			-- record path
			set theFile to _record's file
			set thePath to POSIX path of theFile
			-- set alias filename based on record's basename
			set theName to do shell script "basename " & thePath's quoted form
			set aliasName to theName & " alias"
			-- make temporary folder and set full path to target alias
			set tempFolder to do shell script "mktemp -d -t 'EFRecordAlias'"
			set tempPath to tempFolder & "/" & aliasName
			-- make the Finder alias
			set tempFolderAlias to (POSIX path of tempFolder)
			set tempFolderAlias to (tempFolderAlias as POSIX file) as alias
			tell application "Finder" to make new alias file at tempFolderAlias to theFile ¬
				with properties {name:aliasName}
			set tempFile to tempPath as POSIX file
			-- get record's properties
			set theContainer to _record's container
			set theTitle to _record's title
			set theFrom to _record's from name
			set theTags to _record's assigned tags
			set theNote to note text of _record
			-- set new properties for the alias
			set theTagNames to {}
			repeat with theTag in theTags
				copy theTag's name to end of theTagNames
			end repeat
			-- import the alias record
			set importResult to import files {tempFile} tag names theTagNames note theNote container theContainer
			set importedRecord to first item of importResult
			set importedRecord's title to theTitle
			set importedRecord's from name to theFrom
		end repeat
	end tell
end tell

EagleFiler 1.9b4 adds official support for displaying the contents of records that are alias files.