How to get the chmod mode number.

Hi,
How can I get the current chmod ### of an item, to set a variable to it, for a script flow like this:


tell application "Finder"
	-- the variable "myItem" is set as an alias earlier in the script.
	set theItem to quoted form of POSIX path of myItem
	-- I need to get the current chmod mode number ### of "theItem".
	set chmodNum to (do shell script "chmod " & "???" & theItem) -- HOW DO I DO THIS?
	do shell script "chmod 777 " & theItem with administrator privileges
	-- do something with "theItem"
	do shell script "chmod " & chmodNum & " " & theItem with administrator privileges -- reset the original chmod.
end tell

Is something like this possible?

Hello.

stat --format=%a /path/to/posix/file

Should do the trick, see man stat for more details.

You may also want to look into umod, which contains the octal setting for how files are created.

Something like this may work better for you–


set bar to quoted form of POSIX path of (choose file) -- just to get a file
set foo to text 4 thru end of (do shell script "stat -f \"%p\" " & bar)

Or make a more permanant solution by saving this shell script as a text file and making it executable. I named it “mode” on my machine and keep it in “/usr/local/bin”. This also makes it available on the command line.

#! /usr/bin/perl
die “usage: mode path-to-file\n” if !$ARGV[0];
printf “%04o\n”, (stat $ARGV[0])[2] & 07777;

To call it from Applescript the form is:

do shell script "/usr/local/bin/mode " & “/posix/path/of/file”

Both of these return permissions in decimal form i.e. 0644

Hello.

I am sorry for having used the GNU version.

I’d actually prefer to just use the four last numbers of the result of the stat command, seeing “0644” not “100644” so I’d personally use something like this:


set modestr to text −4 thru −1 of ( do shell script "/usr/bin/stat -f \"%p\" " & pxfilename )

(Maybe with in a try catch block.

Yes, it does work better. I’ll go for the permanent solution once I get the ### format consistently. From my testing:

set chmodNum to (do shell script "stat -f \"%p\" " & theItem)
”> result = 100644 for a normal file, or 40755 of for an app owned by root, group wheel.  As a consequence,
set chmodNum to text 4 thru end of (do shell script "stat -f \"%p\" " & theItem)
”> result = 644, but 55 respectively.

I changed it a bit to get the last three digits:

set chmodNum to text -3 thru end of (do shell script "stat -f \"%p\" " & theItem)
”> result = 644, 755 respectively.  So, for the time being, I'll use this in my script.

Could you please change your perl script accordingly. I do prefer it as a permanent solution.
Many thanks,
Chris

Hello.

Here is my version, that simulates the GNU stat command. I take the numbers from the end deliberately, as I am unsure whether the number of numbers are fixed or not for the stat command.


set modnumber to (do shell script "/usr/bin/stat -f "\%p\" & quoted form of pxFn & " |/usr/bin/sed -n 's/.*\\(..\\)/\\1/p' )

Hello

I apologizes but this one doesn’t compile (at least under 10.8.2).

The first problem is that you typed "% which I guess must be "% but when this typo is corrected, the editor flags the vertical bar.

Yvan KOENIG (VALLAURIS, France) vendredi 8 février 2013 11:23:09

Hello Yvan.

I’m sorry, it was a missing hyphen as well.

set modnumber to (do shell script "/usr/bin/stat -f \"%p\" " & quoted form of pxFn & " |/usr/bin/sed -n 's/.*\\(..\\)/\\1/p' ")

Well, wouldn’t you know, I’ll test the code in the editor from now on. I am glad that you made me conscious about it.

That would be:

#! /usr/bin/perl
die “usage: mode path-to-file\n” if !$ARGV[0];
printf “%03o\n”, (stat $ARGV[0])[2] & 00777;

Now it compiles but


set pxFn to POSIX path of ((path to desktop as text) & "get name of application.app")

set modnumber to (do shell script "/usr/bin/stat -f \"%p\" " & quoted form of pxFn & " |/usr/bin/sed -n 's/.*\\(..\\)/\\1/p' ")
--> ""

returns a magnificient “” which, I guess, is not what it is supposed to do.


set theItem to quoted form of POSIX path of ((path to desktop as text) & "get name of application.app")

set chmodNum to (do shell script "stat -f \"%p\" " & theItem)
--> result = 100644 for a normal file, or 40755 of for an app owned by root, group wheel.  As a consequence,
set chmodNum to text -3 thru end of (do shell script "stat -f \"%p\" " & theItem)
--> result = 644, or  755 respectively.

Returns the correct value with a syntax that I may understand :wink:

Yvan KOENIG (VALLAURIS, France) vendredi 8 février 2013 17:23:14

Hello.

I think that what did happen was that bbcode turned three of my dot into the character elipses, that looks like three dots.

And by all means whatever way you do it, as long as you do it from behind when it comes to the mode of a file.

I think the number can grow larger with sticky bits, or maybe the modes of sockets, named pipes, and other special files. Now that is’nt what we are supposed to do from Applescript anyways, but it is still good to adhere to something that can work everywhere.