Revelando archivos en el Finder desde el Terminal utilizando AppleScript

Una de las mejores y más únicas características de macOS (nacido como Mac OS X, y transitoriamente como OS X) es su capacidad de combinar el interfaz clásico para navegación de archivos del Mac, el Finder, con el Terminal, y el soporte para ese lenguaje de pegamento y automatización que es AppleScript.

A veces uno realiza una búsqueda en terminal con comandos como find, grep, locate, o mdfind (el interfaz de terminal al motor de búsqueda Spotlight), pero quiere poder ver esos archivos en el Finder. Un podría usar la opción -R en el comando open, pero eso sirve sólo para un único archivo/carpeta (aunque la documentación dice que sirve para múltiples archivos). ¿Cómo hacerlo para múltiples archivos?

La clave está en AppleScript: el diccionario de comandos del Finder contiene un comando reveal_files al que se le puede pasar una lista de archivos. Como los archivos en terminal van separados por barras inclinadas (/), es necesario agregar as POSIX file al final de cada uno.

Con esa precaución, he creado una función reveal_files que lee sobre toda la lista de argumentos ($@), y para cada archivo ($file) genera la lista de AppleScript para después, con ella, llamar al comando osascript con el comando tell application "Finder" to reveal files {/path/file1 as POSIX file, /path/file2 as POSIX file, /path/file3 as POSIX file…}, que instruye al Finder a que muestre tantas carpetas como sea necesario, con los archivos que pertenezcan a cada una seleccionados.

El código completo del script podéis verlo a continuación (o en este link al Gist reveal_files.sh). ¡Espero que os sea útil!

https://gist.github.com/juandesant/8c288d7ac7d8578b53799fadfe34dfa8

Actualización 2025-12-18: El script no sirve si no le pasas archivos que estén con toda la ruta completa. Por ejemplo, reveal_files MiArchivo1.pdf MiArchivo2.pdf no funcionaba con la versión anterior, así como tampoco reveal_files *.pdf. Con la actualización de hoy (añadido una comprobación de si el archivo comienza con el carácter “/”), se añade el camino al path actual en el momento de llamar la utilidad.

Otra forma de utilizarlo es utilizar la utilidad find de forma intermedia:

reveal_files $(find $(pwd) -iname "*.pdf")

Al pasar $(pwd) como primer argumento a find todos los path van a empezar for “/”, y la búsqueda con -iname es insensible a mayúsculas/minúsculas. Esta fórmula sí era compatible con la versión anterior del script, pero también se puede utilizar con la actualizada.

#AppleScript #bash #Finder #osascript #scripting #TerminalApp #zsh
Introduction to AppleScript Language Guide

Defines the AppleScript scripting language. Includes many brief sample scripts.

I've been hacked last Monday. A scammer fooled my trust to make me install a malware on my Mac. Which did its malware job. I spent hours changing all my internet passwords and blocking my credit cards when I realized what had happened...
But then I counterattacked : I found the malware program, and analyzed it.
I ran it again inside a debugger (!) and after a long fight I found its embedded encrypted AppleScript script. So now I know what it stole and how it did it. (Quite bad but not as bad as I feared). I even know to which server it sent my data and the userID of the scammer.
How can I contribute to a better world by providing my analysis (the programs, the script, the servers, the process I used) to people who know what to do with it ? Not a company selling anti-malware but a FLOSS community.
Thanks

#phishing #scam #osascript #betterworld

Today I learned how to create a Finder alias using AppleScript and the command line:

```bash
$ osascript -e 'tell application "Finder" to make alias file to posix file "/opt/homebrew/opt/emacs-plus@29/Emacs.app" at posix file "/Applications" with properties {name:"Emacs.app"}'

alias file Emacs.app of folder Applications of startup disk
```

This is for the particular case of creating an Emacs.app named alias at the Applications folder for the Homebrew version of the `emacs-plus` package, but changing the paths above will allow you to create arbitrary Finder aliases from the command line.

#TIL #TodayILearned #AppleScript #Finder #alias #CLI #CommandLineInterface #osascript