is there a better way of writing this simple script?

Hi

I wrote this little script to just switch access on and off a folder
It works! but just wondered if there was a more compact way of writing it.
i’m just learning applescript and i think i feel like i’m writing repetitive lines of code alot,
when i see other peoples scripts they look alot more compact(most of the time)

set folder_to_add_perms to alias "some folder on your mac"
tell application "Finder"
	set the perms to read write
	set the perms1 to none
	if the owner privileges of folder_to_add_perms = read write then
		set owner privileges of folder_to_add_perms to perms1
	else
		set owner privileges of folder_to_add_perms to perms
	end if
	if the group privileges of folder_to_add_perms = read write then
		set group privileges of folder_to_add_perms to perms1
	else
		set group privileges of folder_to_add_perms to perms
	end if
	if the everyones privileges of folder_to_add_perms = read write then
		set everyones privileges of folder_to_add_perms to perms1
	else
		set everyones privileges of folder_to_add_perms to perms
	end if
end tell

any guidance will be very much appreciated.

cheers

And what a great feeling that can be, pidge - right? :smiley:

Yeah, know what you mean… :confused:

If I’ve interpreted your aim correctly (and assuming there are no issues over setting privileges), I suppose you could try something like:

tell application "Finder" to tell folder (choose folder)
	set l to {read write, none}
	set p to l's item (1 + ((owner privileges is l's item 1) as integer))
	set {owner privileges, group privileges, everyones privileges} to {p, p, p}
end tell

You see thats exactly what i’m talking about!!
i would of never thought to do it that way!

Your spot on with that Kai.
Proves my point exactly…

Thanks Kai!!

Is there anything i need to to be doing do you think that might help me optimize my code, or is
it just something that comes with more experience?

cheers

You’re most welcome, pidge.

Actually, from what I’ve seen, you seem to be doing just fine as you are. The main hurdle is to produce scripts that actually work - and that’s exactly what you’re doing.

I don’t think there’s a single piece of advice I could offer - apart from continuing to come back here, following the discussions that interest you - and asking questions when you’re unsure about something. The really good thing about this site is that there’s a healthy mix of knowledge and abilities, so there’s usually no shortage of interesting questions - or of correspondingly creative solutions. And those around here who know a thing or two about AppleScript are usually willing, if asked, to go into considerable detail about the finer points of condensing scripts, enhancing their performance and efficiency - or improving various other aspects, such as robustness and reliability, portability or readability.

As you’ve probably noticed, scripting styles can vary quite widely. AppleScript’s syntactic flexibility allows a fair amount of individual expression, and it’s a good idea to search out script examples that look particularly interesting - especially those that aren’t immediately clear (perhaps because they’re heavily condensed, or use unfamiliar techniques). Then pull them apart and examine individual expressions and statements to figure out, if possible, how they work (rather like trying to discover about the internal mechanism of an old clock). For me, anything gleaned from experimenting in this way seems to stick rather better than any amount of material that I might read. Even so, when putting it all back together again, there may still be some parts that remain somewhat baffling (again, just like the clock analogy). That’s a good time to ask for help - and someone around here (possibly even the guy who wrote the original script) may be able to help fill in any gaps.

You undoubtedly put your finger on it when you suggested that the ability to optimise code might be something that comes with experience - much the same as musicians improve with practice, or athletes perform better through training. We might have good and bad days - but the combination of a little diligence, perseverance and time is usually a pretty good recipe for success…
:slight_smile:

thanks for your help Kai.

i like the clock analogy i just feel like i’ve got big ben in bits sometimes…

cheers

:lol:

Hi,

The thing about scripts is not how compact it is, but how much of it you understand. Do you want somebody to write scripts for you forever?

The question is, how can you write a script, so the other person can learn and not come back 1 week later with the same question.

A lot depends on the the poster, but a lot depends on the teacher.

gl,

Not sure I understand the point you’re trying to make, kel. Care to clarify it?