33

I thought this feature was only for apps, not for documents.

This is a C# script file, and I've told it to open it with Visual Studio Code. I can open VS Code with no problem, so it doesn't seem to be the permissions on that.

enter image description here

6 Answers 6

50

This answer applies if you set the app for that particular document, not for all documents of that type (e.g. by using Get Info on the document, and changing the "Open with" pop-up menu, but not clicking the "Change All" button).

There are two critical pieces here. First, if you set a document to open in a particular app, what it actually does is attach some metadata to the file (think of it like sticking a post-it note onto the file) that says to open it with that particular app. Second, if a document is quarantined (because it was downloaded from the internet, or created by a sandboxed app, or some other reasons), it's considered untrusted, and the gatekeeper security policy will be applied to anything like executable in the file.

These two features can interact in an unfortunate way: that "open with" note is an instruction about what to do, and therefore (sort of) executable code, and therefore the gatekeeper security policy applies to it (even though you created the note, it's "part of" the untrusted file, and therefore untrusted). Thus, double-clicking a document (with "open with" metadata) can get you the untrusted app warning/error.

Fortunately, as long as you want all files of that type (.cs in this case) to open in the same app, there's a solution: in the Info window, Open with section, select the app you want (e.g. Visual Studio Code), and then click "Change all". This removes the metadata from the file, and instead makes an entry in your Launch Services preferences saying that you prefer to have files of that type open in that app. Since the setting is now a personal setting rather than a note attached to an untrusted file, the problem goes away.

6
  • 2
    If you control-click and select "open" on the file in question, and then agree that it is trustworthy in the resulting dialogue, I think it will open as desired in all future cases as the system will trust it in the future - even if the "change all" dance has not been done.
    – j-beda
    Sep 3, 2016 at 15:25
  • 5
    @j-beda If you hold down the Option key after bringing up the context menu for a file, the Open With item changes to Always Open With.
    – PseudoSu
    Sep 7, 2016 at 20:07
  • @PseudoSu is right. I created an .xlsx file with LibreOffice and first simply right-clicked - "open with", then checked "always open with" in the new window (careful: in Windows this changes the default app for the other files too!) but it still displayed the "can't be opened because..." message afterwards. To get rid of it I had to option-right-click and choose "always open with", then hit "open" in the "are you sure" dialog.
    – Neph
    May 23, 2019 at 13:26
  • Funnily enough, checking the 'Change all' box is not working for me - I have ~6000 files all the same format and have to go through the same procedure of selecting which application to open with for each one
    – stevec
    Jun 27, 2020 at 4:15
  • Same problem here, I usually work with LibreOffice, but for a certain project I am forced to use MS Office. Files usually come as mail attachments. This answer explains some of the intricacies involved here. But the solution I use is the one I found in another answer below: switch off the quarantine attribute with `xattr -d com.apple.quarantine <file>' for each file I put into my project folder AND use 'always open with' to switch to MS Office for that file. Each of this alone does not do the job. This is a bit inconvenient once, but it makes handling the file easy further on.
    – Stefan
    May 14, 2021 at 9:32
11

This is probably because the file was downloaded from the internet; Safari marks it as quarantined to make it harder for malware to execute.

You can test this easily by doing xattr <file> and checking if one of the output lines is com.apple.quarantine.

If it does, the solution is simply to do xattr -d com.apple.quarantine <file>. Note that executing that command for a file not in quarantine will give an error that the attribute was not present. So it is safe to just start with that; if the warning is given, look for another solution.

3
  • This is the correct answer.
    – Rick
    May 17, 2019 at 22:29
  • THIS - absolutely is the correct answer!
    – Tony
    Jul 9, 2019 at 13:36
  • xattr -d didn't work for me in Mojave on a Bash script. It didn't complain, but didn't remove the attribute either. But using -c ("clear") instead, worked : xattr -c YourFile.
    – mivk
    Sep 15, 2021 at 16:26
8

The document is likely marked as executable.

You need to remove the executable bit from the file. This does not affect the file's contents, only how it is handled by the operating system. You can do this with the command line tool chmod:

chmod -x <path to file>

File Marked as Executable

All executable files pass through macOS's security checks. As this file is not a signed executable, the checks fail.

As this is a mistakenly marked-as-executable file, the ability to open bypassing the checks are disabled; there is no executable to actually launch.

This problem is fairly common when moving files between operating systems. Differences in the importance of file type bits effectively get lost in translation.

6

I was facing this problem for all .php files.

It can also happen for files with other extensions.

If you are using MacOS High Sierra or Mojave- All you need to do is right-click on any of the file > Get info > Open with (here choose the app you want to open the file with)> Click on change all.

enter image description here

That's all you need to do. Now all files with this extension will open easily.

4
  • This is not a correct answer. The method you describe here is for opening documents which do not have an app associated with them through LaunchServices, not documents or apps which are disallowed from opening through the macOS Security framework.
    – Almo
    Mar 27, 2019 at 21:10
  • @Almo , No this method is not for "opening files which do not have an app associated with that". I wrote this answer because that's exactly the problem what users are facing and searching in google for the solution. This little tweak helps to fix what exactly they are looking for. Mar 27, 2019 at 22:08
  • Um, yeah, it is. Your solution did not help my case because it was a security issue, not a "which app to open with" issue, which is what you've done.
    – Almo
    Mar 27, 2019 at 23:50
  • Curiously, this path happened to be the only way to bypass the security error that worked for me (for opening .json files with Visual Studio Code in my case). For some reason, the "Open with" > "Change All" path did not seem to set the default app and the error kept showing up.
    – Samuel
    Dec 25, 2019 at 17:31
0

Your Mac is protecting you from "unidentified developers" by default. By default you can't run apps that aren't downloaded from the app store. Try this: open Preferences->Security & Privacy->General, change "Allow apps downloaded from:" to "Anywhere". That should solve message from coming up.

4
  • 2
    A .cs file is not an app. The app opening the file is already trusted, as stated in the question. See the accepted answer.
    – Almo
    Sep 1, 2016 at 17:06
  • Unless it is an app and someone changed the extension to trick you into opening it. Sep 2, 2016 at 4:00
  • 1
    But if you change a .app to something else, it won't open as an app because .app is a folder. Once you rename it, it just behaves as a normal folder.
    – Almo
    Jan 10, 2017 at 13:44
  • This will prevent the messages from appearing, but will not resolve the root of the problem. Asker presumably does not want to disable Gatekeeper completely. Nov 29, 2018 at 22:16
-1

Try right clicking on the file and choosing "Open with...", and opening it with the application you want to use. This time, you should get an "open anyway" option. After you've done that once, you should be able to change the associated application for that file (alone) and have it open without problems.

2
  • This is not a correct answer. The method you describe here is for opening documents which do not have an app associated with them through LaunchServices, not documents or apps which are disallowed from opening through the macOS Security framework.
    – IconDaemon
    Feb 15, 2018 at 19:15
  • Well, I had the same problem as the OP, with a different application: I had a particular PDF that opened in Preview but I wanted it to open in Acrobat Pro; when I changed the associated app for that document and double-clicked it, I got the security block. When I right-clicked it and chose "open anyway" once, I could then double-click it without problems. (I had also made the PDF stationery--perhaps that was also involved?)
    – Read
    Feb 17, 2018 at 20:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .