File Visible/Invisible

I was kludging around with file visibility and came up with this, which seems awkward at best. Isn’t there a neater way to toggle visibility (since the Finder won’t let you set it)?

set F to choose file
set N to displayed name of (info for F)
set FT to F as string -- address as text
set tFolder to text 1 thru ((offset of N in FT) - 1) of FT -- container without Finder
if visible of (info for F) then
	-- make invisible
	set Finvis to quoted form of POSIX path of tFolder & "." & N -- leading period
	set Fvis to quoted form of POSIX path of F
	do shell script "mv " & Fvis & space & Finvis
else
	-- make visible
	set Fvis to quoted form of POSIX path of tFolder & text 2 thru -1 of N -- Remove "."
	set Finvis to quoted form of POSIX path of F
	do shell script "mv " & Finvis & space & Fvis
end if

Rather than actually making the file/folder invisible, you might relocate it into a known invisible folder with the destination path.

Hi Adam,
A one-liner neat enough? :wink:

set theFile to choose file
tell application "System Events" to set visible of theFile to (not visible of theFile)

Be great, QD if it worked for me. I duplicated a file to my desktop, ran the script, chose my duplicate, and nada. It’s still sitting there. :frowning:

This is a fairly simple version, I suppose:

set theFile to (choose file)
set currentName to name of (info for theFile)
if (currentName begins with ".") then
	tell application "System Events" to set theFile's name to text 2 thru -1 of currentName
else
	tell application "System Events" to set theFile's name to "." & currentName
end if

. or, if you really must:

set theFile to (choose file)
tell name of (info for theFile) to set newName to text (1 + ((it begins with ".") as integer) * 2) thru -1 of ("." & it)
tell application "System Events" to set theFile's name to newName

Now that’s more like it. Thank you Mr. G. :slight_smile:

D’oh! Why didn’t I use System Events?

I came up with almost the same script (even had “currentName” for the variable) - but I used Finder, which then had to be relaunched.

I did not come close to your shorter version.

Precisely the problem I had, CapitalJ. Since Mr. G has usually eschewed the Kai-patented short forms before Kai went missing some months ago, perhaps his short form is in memorium. Even if not, it’s neat.

This is interesting. It never dawned on me that the truth of an item could be made a number.

FYI: Neither of Nigel’s scripts worked properly on my setup tested under 10.3.9. I was able to make things invisible with the name cheat, but their visibleness was not enabled after a second run; the name was changed both times. They worked fine with 10.4.10 on my home machine, however.

Truly Slick, in my view, but not the first time I’ve seen Nigel use it.

set {T, F} to {true as integer, false as integer} --> {T, F} --> {1, 0}

Kai wouldn’t have sacrificed efficiency for a shorter script. [1] His only option here would have been to use one-letter variable names. :wink: I wonder what happened to him. Hope he’s OK. :confused:

[i][1] My shorter script does more work than the longer to derive the new name from the original:

Longer: A text comparison, followed by either a text extraction or a text concatenation.

Shorter: A text comparison, followed by a boolean-to-integer coercion, a multiplication, an addition, a text concatenation, and a text extraction.

The Three Great Priorities of Scripting “ as formulated by me for a never-finished article, but broadly in line with the ideas of earlier and greater thinkers “ are:

First Priority: A script should work. It should do all that it’s meant to do and not do anything it’s not meant to do. If intended for general release, it should be effective and foolproof over a wide range of systems and circumstances and shouldn’t make parochial assumptions about OS versions, date formats, decimal point characters, disk names, TID values, [/i]etc.[i] It should use legal methods and not rely on undocumented behaviour or bugs in the software being scripted “ although it will often need to work round these.

Second Priority: A script should perform as efficiently as is reasonably possible “ unless this conflicts with the First Priority or simply isn’t worth the bother in the circumstances. However, a script may initially and temporarily be pardoned optimal performance if the development time saved allows it to be in use earlier and the First Priority isn’t compromised.

Third Priority: A script should be reasonably easy to read, to understand, and to edit “ unless this conflicts with the First or Second Priorities. Other people may need to understand it in order to use, adapt, debug, evaluate, learn from, or improve it. Even the original author may want to come back to it later when he or she’s forgotten how it works.

To criticise the System Events scripts from these aspects:

  1. Both scripts work in Tiger, but neither work in Jaguar. I see from Marc’s post that they’re not reliable in Panther either.

  2. Using System Events doesn’t seem to be quite as efficient as other methods, but it’s probably good enough here “ especially since the object was to find a simpler method.

  3. The shorter script is both unnecessarily inefficient and unnecessarily obfuscated and suffers from a surfeit of smartarsedness.[/i] :wink: