I am trying to make a script that finds and deletes appex files, the anoying extensions that get injected into Safari or Finder.
I’ve managed to get it working, but it’s kind of clumsy. I couldn’t get Finder to find appex files inside apps, so I used the find commnad. I can’t use -user to exclude Apple apps because the apps installed from the AppStore belong to root, just like Mail, Safari and others. So I used multiple grep strings.
Any suggestions on improving the script are welcomed.
set theList to (do shell script "find /Applications -name \"*.appex\" | grep -v \"Books.app\" | grep -v \"Notes.app\" | grep -v \"Mail.app\" | grep -v \"Home.app\" | grep -v \"Safari.app\" | grep -v \"FaceTime.app\" | grep -v \"Photos.app\" | grep -v \"Messages.app\" | grep -v \"VoiceMemos.app\" | grep -v \"Reminders.app\" | grep -v \"Calendar.app\" | grep -v \"iTunes.app\"")
display dialog ("The following .appex files were found:
" & theList) buttons {"Select appex files", "Cancel"}
if button returned of result = "Cancel" then
return
else
set thePrgs to paragraphs of theList
set appexFiles to {}
repeat with i from 1 to count of thePrgs
set end of appexFiles to POSIX file (item i of thePrgs)
end repeat
tell application "Finder"
repeat with currentFile in appexFiles
set question to display dialog "Do you want to delete" & (POSIX path of currentFile) buttons {"Yes", "No", "Cancel"} default button 2
set answer to button returned of question
if answer is equal to "Yes" then
try
tell application "Finder"
delete currentFile
end tell
on error
display dialog ("File could not be deleted") buttons {"OK"}
end try
end if
if answer is equal to "Cancel" then
return
end if
end repeat
end tell
end if
If you delete anything inside application bundles, you invalidate their code signature. In turn, that could lead them to fail to launch – newer versions of the OS are likely to check more often. It’s a pretty risky think to do. Why not just turn them off in System Preferences?
It’s for anyone to decide what they do, but it’s not for anyone to decide which makes more sense. That depends on the facts. You might want to read this for an idea of potential problems you can cause yourself:
I can’t speak to your task’s end advisability, as I don’t really understand what you’re talking about, however, your method may affect numerous unintended and irreversible changes.
I believe this obtains your desired result, but proceed with caution:
set theList to (do shell script "find " & (path to applications folder)'s POSIX path's quoted form & " -maxdepth 5 -type d -name '*.appex' | grep -Ev '(Books|Notes|Mail|Home|Safari|FaceTime|Photos|Messages|VoiceMemos|Reminders|Calendar|iTunes).app' ")
I would like to have a list of extensions hidden in applications. But for some reason your code is very slow. I think here you can do without the do shell script and get the best results. I especially would like to kill all the processes of the CoreSimulator of Xcode. How can I get rid of them?
My version returns only
“/Applications/IINA.app/Contents/PlugIns/OpenInIINA.appex
/Applications/BetterZip.app/Contents/PlugIns/FinderSyncExtension.appex”
set list_of_apps to {"Books", "Notes", "Mail", "Home", "Safari", "FaceTime", "Photos", "Messages", "VoiceMemos", "Reminders", "Calendar", "iTunes"}
set appexFiles to {}
set appsPath to (path to applications folder) as string
repeat with theItem in list_of_apps
set theBundle to (appsPath & theItem & ".app:Contents:") as alias
getAllFiles(theBundle, appexFiles)
end repeat
appexFiles
on getAllFiles(theFolder, appexFiles)
tell application "System Events"
set fileList to every file of theFolder whose name extension is "appex"
repeat with i from 1 to (count fileList)
set end of appexFiles to item i of fileList
end repeat
set subFolders to folders of theFolder
repeat with subFolderRef in subFolders
my getAllFiles(subFolderRef, appexFiles)
end repeat
end tell
end getAllFiles
OK, try this (1 minute 56 seconds for processing totally 84 apps I have installed):
with timeout of 200 seconds
set list_of_apps to paragraphs of (do shell script "find /Applications/ -name \"*.app\" -maxdepth 2| sed -e \"s/\\(.*\\)\\/\\([^\\/]*\\).app/\\2/g\"")
set pre_Apps_List to {"Books", "Notes", "Mail", "Home", "Safari", "FaceTime", "Photos", "Messages", "VoiceMemos", "Reminders", "Calendar", "iTunes"}
set appexFiles to {}
set appsPath to (path to applications folder) as string
repeat with theItem in list_of_apps
if theItem is not in pre_Apps_List then
try
set theBundle to (appsPath & theItem & ".app:Contents:") as alias
getAllFiles(theBundle, appexFiles)
end try
end if
end repeat
appexFiles
end timeout
on getAllFiles(theFolder, appexFiles)
tell application "System Events"
set fileList to every file of theFolder whose name extension is "appex"
repeat with i from 1 to (count fileList)
set end of appexFiles to item i of fileList
end repeat
set subFolders to folders of theFolder
repeat with subFolderRef in subFolders
my getAllFiles(subFolderRef, appexFiles)
end repeat
end tell
end getAllFiles
NOTE: I added to code try block, which does trick - it avoids automatically apps you can’t change contents (such as most of Apple apps and MicroSoft apps). Simply in such case command as alias fails and we use that fact
I edited my post, as there was an oversight on my part that you were looking for inverse results. The extended regex operand, -E, was added to enable the or symbols. The search’s depth was limited for better speed, however, additional caveats apply because of this; that number is arbitrary, as I don’t know how deep your objects are.
Thank you, that’s much better. maxdepth 5 works well for apps within a folder in Applications.
(POSIX path of (path to applications folder)) can be used instead of (path to applications folder)'s POSIX path’s quoted form
However, I realized that grep excludes all apps that end in the provided string. So, an app called CropPhotos.app is excluded because Photos.app is in the list.
In order to prevent unintentionally capturing words ending with the pattern, you could use find’s prune to spare out specific items, which starts becoming a bit convoluted, or just add the w option to the grep to make it function more literally.
set theList to (do shell script "find " & (path to applications folder)'s POSIX path & " -path 'cropPhotos.app' -prune -o -maxdepth 5 -name '*.appex' -print | grep -Ev '(Books|Notes|Mail|Home|Safari|FaceTime|Photos|Messages|VoiceMemos|Reminders|Calendar|iTunes).app' ")
set theList to (do shell script "find " & (path to applications folder)'s POSIX path's quoted form & " -maxdepth 5 -type d -name '*.appex' | grep -Evw '(Books|Notes|Mail|Home|Safari|FaceTime|Photos|Messages|VoiceMemos|Reminders|Calendar|iTunes).app' ")