Hi,
This is the line “doing the setting” in a script which is setting the label index of items (files and/or folders) in the target folder, to match the labels of the items with matching names in the source folder.
set label index of aNameTarget to LabelIndexSource
This works with any items with normal permissions but, fails when it finds items with special permissions in the target folder.
How do I set the label index using a do shell script command, to allow me to append “with administrator privileges”?
I think the nicest way to do it, would be to create a script bundle, and then execeute a shell script with a reference to the script in your script bundle’s resources folder.
Here is a shell script you can look at, that takes parameters from the command line, (maybe with spaces in), and passes them over to Applescript via the osascript command. The script as such was originally made by Nigel Garvey.
#!/bin/bash
osascript <<END! - "$@"
on run {pathToPDF, pageNum}
set pdfFile to pathToPDF as POSIX file
tell application "Preview" to open pdfFile
tell application "System Events"
tell application process "Preview"
set frontmost to true
-- Execute an option-command-"g" keystroke to get the "Go to page." dialog.
keystroke "g" using {option down, command down}
-- The dialog's a sheet in Preview 5.0.3. Wait for it to appear.
repeat until sheet 1 of window 1 exists
delay 0.2
end repeat
-- "Type in" the page number and press Return.
keystroke pageNum
keystroke return
end tell
end tell
end run
END!
I hope this helps.
Edit
You must make the shell script executable by issuing chmod gu+x and the shell script should be saved as utf-8 no BOM. The paths to the files you want to set attributes for can really be sent as hfstext parameters, so you won’t have to perform any unnecessary conversions back and forth.
Honestly, I don’t understand how I could use this.
As I said, what I really need is a do shell script command to replace my pure AS command quoted in my initial post. Basically, I need a do shell script command to set the label index of a file or folder.
I did thing of enclosing the "set label index . " line in a chmod block but, I discarded the idea because:
(1) it looks like a kludge to me;
(2) the “set label index .” line is in a repeat loop to handle folders with up to 400 items. Repeating the chmod block that many times can’t be very efficient;
(3) if the script fails or is interrupted for some reason, it could leave an unknown item with the wrong permissions.
There must be a direct do shell script command to set the label index of an item and run it with admin privileges.
The chmod was for setting the executeable permissions for the shell script, so that it will be executable for do shell script line that executes it with administrator privileges.
There is nothing that hinders you from coding the whole set attribute thing inside the osascript of the shell script, and that the only things that happens in your original AppleScript is setting the folder to change the label indexes in, and the line with the do shell script (with Administrator Privileges), that effectively does it.
McUsrII, thanks for trying to help me but, i’m ashamed to reveal publicly this obvious reality, my shell scripting skills are not really up to the task. Can you point me to a place I can read specifically about “coding the whole set of attribute”?
That’s exactly the help I need, the [do shell script " . " with Administrator Privileges] line to change the label attribute. Can anybody help me with it?
If you look at the script above, you’ll see that it is a regular Applescript embedded within an osascript block.
The osascript is configured to take parameters from the commandline. Further upwards you’ll find a sheebang, that tells the operating system that this is a bash shell script.
Now, just take that script, and replace it with a tried Applescript with a run handler that takes one parameter, the path of the folder from which you want to replace the label indexes.
If the run handler works up front with taking a folder as a parameter, while inside and Applescript, it should work equally well from within the shell script.
When you have got the shell script saved onto say your desktop, then open a terminal window, and
cd ~/desktop
chmod gu+x "shellscript"
Then try to execute the shell script by specifying: ~/Desktop/shellscript folderthatisparameter.
If that gave some results, exept for the files you lack privileges for, then you can put the script into the content’s resources of a script bundle, and specify it by the path to command in the applescript that does the do shell script.
I hope all of this made some sense. The most important thing is to debug your before you stuff it into the osascript inside the shell script. Another thing is that you have to specify the path to a shellscript in order to execute it, if the folder the shell script is in,isn’t in a path.
There are a lot of good tutorials on shell scripting out there, if you google for them.
Hi, yes, it does make sense. I do understand it in principal but, it will take me more time to work it out in detail, than I really have right now. And yes, I did spend quite some time searching both this forum and google others, specifically for a do shell script command to set the label index for an item. Not much luck.
I did find a script by James Nierodzik at #3 in http://macscripter.net/viewtopic.php?id=25499 but, I’m still straggling to understand and adapt the syntax for my script. Any help with it would be greatly appreciated.
Unfortunately, I need a quick way of adapting my scrip to handle special permissions, as I have to install it on 31 Macs I’m supporting.
This is my current script:
set sourceFolder to (path to documents folder as text) & "Job Templates:Label Templates:"
set targetDefLoc to (path to documents folder as text) & "Jobs:"
set targetFolder to choose folder default location alias targetDefLoc with prompt "Choose the TARGET Job folder to set the labels."
set targetFolderPath to targetFolder as string
tell application "Finder"
set sourceFileNames to name of every item of folder sourceFolder
set targetFileNames to name of every item of folder targetFolder
repeat with aName in sourceFileNames
set aNameSource to alias (sourceFolder & aName)
set LabelIndexSource to label index of aNameSource
if aName is in targetFileNames then
set aNameTarget to alias (targetFolderPath & aName)
-- This is the offending line, which is crashed by items with special permissions. I could go around them with a "try/on error/end try" block, but then the items left out would need be handled manually. Not good!
set label index of aNameTarget to LabelIndexSource
end if
end repeat
end tell
I’m still hoping that there is a quick [do shell script " . " with Administrator Privileges] replacement for the offending line.
Any saviour out there?
I have converted your script into an Applescript that is to be saved as a bundle, and a shell script you will put into the bundle’s resources folder. You will have to visit the resources folder or change the permissions from the commandline up front as specified above.
You’ll also have to figure out the correct incantation for the path to resource command. It should return an alias though, before it is coerced to a posix path. But you’ll have to test that it works before removing the log statement, and return statement.
set sourceFolder to quoted form of (path to documents folder as text) & "Job Templates:Label Templates:"
set targetDefLoc to (path to documents folder as text) & "Jobs:"
set targetFolder to choose folder default location alias targetDefLoc with prompt "Choose the TARGET Job folder to set the labels."
set targetFolder to quoted form of targetFolder as string
set flexiLabel to quoted form of posix path of (path to resource "flex20labeler" )
log flexiLabel
return
-- you must work out that line yourself
do shell script flexiLabel & " " & sourceFolder & " " targetFolder with administrator privileges
The other part is the flex20Labeler shell script. (utf8 no BOM)
#!/bin/bash
osascript <<END! - "$@"
on run {sourceFolder, targetFolder}
tell application "Finder"
set sourceFileNames to name of every item of folder sourceFolder
set targetFileNames to name of every item of folder targetFolder
repeat with aName in sourceFileNames
set aNameSource to alias (sourceFolder & aName)
set LabelIndexSource to label index of aNameSource
if aName is in targetFileNames then
set aNameTarget to alias (targetFolderPath & aName)
-- This is the offending line, which is crashed by items with special permissions. I could go around them with a "try/on error/end try" block, but then the items left out would need be handled manually. Not good!
set label index of aNameTarget to LabelIndexSource
end if
end repeat
end tell
end run
END!
I hope you can make this work for you.
Hi, many thanks for your help. I think I’m close but not quite there yet. This is what I’ve done:
The flex20Labeler shell script, saved as Unicode (UTF-8) and chmod gu+x.
It is placed in the “Resources” folder of the Set Job Labels script bundle.
I note that I removed my comment about the offending line and, as in my original AS script, every path in it is in Mac format.
#!/bin/bash
osascript <<END! - "$@"
on run {sourceFolder, targetFolder}
tell application "Finder"
set sourceFileNames to name of every item of folder sourceFolder
set targetFileNames to name of every item of folder targetFolder
repeat with aName in sourceFileNames
set aNameSource to alias (sourceFolder & aName)
set LabelIndexSource to label index of aNameSource
if aName is in targetFileNames then
set aNameTarget to alias (targetFolderPath & aName)
set label index of aNameTarget to LabelIndexSource
end if
end repeat
end tell
end run
END!
This is the Set Job Labels script bundle and where I still need help:
set sourceFolder to (path to documents folder as text) & "Job Templates:Label Templates:"
--> HD-1:Users:chris:Documents:Job Templates:Label Templates:
set targetDefLoc to (path to documents folder as text) & "Jobs:"
set targetFolder to choose folder default location alias targetDefLoc with prompt "Choose the TARGET Job folder to set the labels."
--> HD-1:Users:chris:Documents:Jobs:myTarget:
set flexiLabel to quoted form of POSIX path of ((path to me) & "Contents:Resources:flex20labeler" as string)
--> '/Users/chris/Desktop/Set Job Labels.scptd/Contents/Resources/flex20labeler'
do shell script flexiLabel & " " & quoted form of POSIX path of sourceFolder & " " & quoted form of POSIX path of targetFolder with administrator privileges -->
(*tell current application
do shell script "'/Users/chris/Desktop/Set Job Labels.scptd/Contents/Resources/flex20labeler' '/Users/chris/Documents/Job Templates/Label Templates/' '/Users/chris/Documents/Jobs/myTarget/'" with administrator privileges
--> error "88:92: execution error: Finder got an error: Can't get folder \"/Users/chris/Documents/Job Templates/Label Templates/\". (-1728)" number 1*)
I included the AS Editor Replies. It works up to and including the do shell script. It asks for the admin password, but then it ends with the error.
From “Finder got an error” it appears that error is in the shell script in the “tell application “Finder”” block.
I can’t see what generates the error. Can you help, please.
First of all, try removing the quoted form and see if that helps.
If that doesn’t work, save the contents of the original script somewhere and paste in the script below.
#!/bin/bash
osascript <<END! - "$@"
on run {sourceFolder, targetFolder}
tell application "Finder"
try
set sourceFileNames to name of every item of folder sourceFolder
on error e number n
my logit("set sourcefilenames" & e & " " & n ,"flex20Label")
end try
try
set targetFileNames to name of every item of folder targetFolder
on error e number n
my logit("set targetfilenames" & e & " " & n,"flex20Label")
end try
repeat with aName in sourceFileNames
on error e number n
set aNameSource to alias (sourceFolder & aName)
my logit("set anameSource" & e & " " & n,"flex20Label"
end try
try
set LabelIndexSource to label index of aNameSource
on error e number n
my logit("set label index source" & e & " " & n,"flex20Label"
end try
if aName is in targetFileNames then
try
set aNameTarget to alias (targetFolderPath & aName)
on error e number n
my logit("set anameTarget" & e & " " & n,"flex20Label"
end try
try
set label index of aNameTarget to LabelIndexSource
on error e number n
my logit("set label index" & e & " " & n,"flex20Label"
end try
end if
end repeat
end tell
end run
to logit(log_string, log_file)
do shell script "echo `date '+%Y-%m-%d %T: '`\"" & log_string & "\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit
END!
Open ~/Library/logs and double click on flex20Label.log, it should open in the Console.app, and tell me what you see.
I assume you refer to the script bundle, not the shell one, which doesn’t have any quoted form in it.
If so, which quoted form do you mean?
The one in “set flexiLabel” is there, as I understand, just for the benefit of the do shell script command and it seems to be accepted without an error.
If you mean the ones on the do shell script command, I already tried that. It also ended after it asked me for the password, but the error was:
.
do shell script flexiLabel & " " & sourceFolder & " " & targetFolder with administrator privileges -->
(*tell current application
path to current application
--> alias "HD-1:Users:chris:Desktop:Set Job Labels 2.1a.scptd:"
do shell script "'/Users/chris/Desktop/Set Job Labels 2.1a.scptd/Contents/Resources/flex20labeler' HD-1:Users:chris:Documents:Job Templates:Label Templates: HD-1:Users:chris:Documents:Jobs:myTarget:" with administrator privileges
--> error "88:92: execution error: Finder got an error: Can't get folder \"HD-1:Users:chris:Documents:Job\". (-1728)" number 1*)
Obviously, it failed to interpret the space in “:Job Templates:”
If there is nothing wrong in what I just said, lets go to your next option:
1st. which “original script” do you mean? ” my original AS script at #8 above, or the AS script before I saved it as a bundle, or the shell script “flex20labeler”, or something else.
2nd. where do I paste it in your debug shell script?
3rd. how do I run it? ” save it as UTF-8 as some name, chmod gu+x and then double click on it.
Please let me know.
Cheers, Chris
Just replace the contents of the shell script you manage to create inside the bundle with the contents in the script above. I am sorry for having been unclear.
You must also put the “quoted form” back into the AppleScript
There is more to it than just that.
when it comes to where you create aliases and what not of the file or folder names given as attribute to your run script. then replace each filename with the construct below:
I’ll deal here only with Quote 1 (to fix that first).
I’ve replaced the shell script in flex20Labeler with your new one from #11 and run the script bundle again. I got this:
set sourceFolder to (path to documents folder as text) & "Job Templates:Label Templates:"
set targetDefLoc to (path to documents folder as text) & "Jobs:"
set targetFolder to choose folder default location alias targetDefLoc with prompt "Choose the TARGET Job folder to set the labels."
set flexiLabel to quoted form of POSIX path of ((path to me) & "Contents:Resources:flex20labeler" as string)
do shell script flexiLabel & " " & quoted form of POSIX path of sourceFolder & " " & quoted form of POSIX path of targetFolder with administrator privileges -->
(*tell current application
path to current application
--> alias "HD-1:Users:chris:Desktop:Set Job Labels 2.2.scptd:"
do shell script "'/Users/chris/Desktop/Set Job Labels 2.2.scptd/Contents/Resources/flex20labeler' '/Users/chris/Documents/Job Templates/Label Templates/' '/Users/chris/Documents/Jobs/myTarget/'" with administrator privileges
--> error "414:416: syntax error: Expected "end" but found "on". (-2741)" number 1*)
It appeared that it got over the Finder path error, but found an error in the new shell script. Here is an indented look of it (I’m using |applescript| instead of |code| blocks to get the indentation):
#!/bin/bash
osascript <<END! - "$@"
on run {sourceFolder, targetFolder}
tell application "Finder"
try
set sourceFileNames to name of every item of folder sourceFolder
on error e number n
my logit("set sourcefilenames" & e & " " & n ,"flex20Label")
end try
try
set targetFileNames to name of every item of folder targetFolder
on error e number n
my logit("set targetfilenames" & e & " " & n,"flex20Label")
end try
repeat with aName in sourceFileNames
on error e number n
set aNameSource to alias (sourceFolder & aName)
my logit("set anameSource" & e & " " & n,"flex20Label"
end try
try
set LabelIndexSource to label index of aNameSource
on error e number n
my logit("set label index source" & e & " " & n,"flex20Label"
end try
if aName is in targetFileNames then
try
set aNameTarget to alias (targetFolderPath & aName)
on error e number n
my logit("set anameTarget" & e & " " & n,"flex20Label"
end try
try
set label index of aNameTarget to LabelIndexSource
on error e number n
my logit("set label index" & e & " " & n,"flex20Label"
end try
end if
end repeat
end tell
end run
to logit(log_string, log_file)
do shell script "echo `date '+%Y-%m-%d %T: '`\"" & log_string & "\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit
END!
It appeared to me that the first try block in the repeat block is wrong and after I “corrected” that, it complained about expecting “,” or “)” but found end of line. Again I “corrected” everything I thought was missing. After two more attempts to fix it, I got this:
#!/bin/bash
osascript <<END! - "$@"
on run {sourceFolder, targetFolder}
tell application "Finder"
try
set sourceFileNames to name of every item of folder sourceFolder
on error e number n
my logit("set sourcefilenames" & e & " " & n,"flex20Label")
end try
try
set targetFileNames to name of every item of folder targetFolder
on error e number n
my logit("set targetfilenames" & e & " " & n,"flex20Label")
end try
repeat with aName in sourceFileNames
try
set aNameSource to alias (sourceFolder & aName)
on error e number n
my logit("set anameSource" & e & " " & n,"flex20Label")
end try
try
set LabelIndexSource to label index of aNameSource
on error e number n
my logit("set label index source" & e & " " & n,"flex20Label")
end try
if aName is in targetFileNames then
try
set aNameTarget to alias (targetFolderPath & aName)
on error e number n
my logit("set anameTarget" & e & " " & n,"flex20Label")
end try
try
set label index of aNameTarget to LabelIndexSource
on error e number n
my logit("set label index" & e & " " & n,"flex20Label")
end try
end if
end repeat
end tell
end run
to logit(log_string, log_file)
do shell script "echo `date '+%Y-%m-%d %T: '`\"" & log_string & "\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit
END!
I can’t see here anything else missing. However, I’m getting the same end error:
.
do shell script flexiLabel & " " & quoted form of POSIX path of sourceFolder & " " & quoted form of POSIX path of targetFolder with administrator privileges
(*tell current application
path to current application
--> alias "HD-1:Users:chris:Desktop:Set Job Labels 2.3.scptd:"
do shell script "'/Users/chris/Desktop/Set Job Labels 2.3.scptd/Contents/Resources/flex20labeler' '/Users/chris/Documents/Job Templates/Label Templates/' '/Users/chris/Documents/Jobs/myTarget/'" with administrator privileges
--> error "566:567: syntax error: Expected "," or ")" but found end of line. (-2741)" number 1
Result:
error "566:567: syntax error: Expected "," or ")" but found end of line. (-2741)" number 1*)
I can’t see anything else to try fixing. Please, can you find what’s still wrong with the shell script and post an updated one.
Thanks,
Chris
EDIT
I didn’t find any “flex20Labeler.log” in ~/Library/Logs. Obviously, none was created.
You had a variable FolderTargetPath, that didn’t exist. Here is a reworked shell script that works at least in normal cases so I have removed all of the try blocks. I only tested it from the commandline, so if it doesn’t work now, then you must remove the two commented out lines that are supposed to remove the quotes from the parameters. As I am not sure whether the do shell script removes them or not. (It probably does.)
Good Luck!
#!/bin/bash
osascript <<END! - "$@"
on run {sourceFolder, targetFolder}
# set sourceFolder to text 2 thru -2 of sourceFolder
# set targetFolder to text 3 thru -2 of targetFolder
# maybe you'll need those two after all!
tell application "Finder"
set sourceFileNames to name of every item of folder sourceFolder
set targetFileNames to name of every item of folder targetFolder
repeat with aName in sourceFileNames
set aNameSource to alias (sourceFolder & aName)
set LabelIndexSource to label index of aNameSource
if aName is in targetFileNames then
set aNameTarget to alias (targetFolder & aName)
set label index of aNameTarget to LabelIndexSource
end if
end repeat
end tell
end run
END!
I’ve probably lost that FolderTargetPath variable some time during the many versions of the split scripts.
Here is the pure AS script which does work very well, albeit only with normal file and folder permissions:
set sourceFolder to (path to documents folder as text) & "Job Templates:Label Templates:"
set targetDefLoc to (path to documents folder as text) & "Jobs:"
set targetFolder to choose folder default location alias targetDefLoc with prompt "Choose the TARGET Job folder to set the labels."
-- set targetFolderPath to targetFolder as string
tell application "Finder"
set sourceFileNames to name of every item of folder sourceFolder
set targetFileNames to name of every item of folder targetFolder
repeat with aName in sourceFileNames
set aNameSource to alias (sourceFolder & aName)
set LabelIndexSource to label index of aNameSource
if aName is in targetFileNames then
-- set aNameTarget to alias (targetFolderPath & aName)
set aNameTarget to alias ((targetFolder as string) & aName)
set label index of aNameTarget to LabelIndexSource
end if
end repeat
end tell
The two commented out lines are required to make it work. Alternatively, the first line in the if block must be as shown. I adopted the latter.
Now, about the combined AS and shell scripting.
This is the flex20Labeler shell script, saved as Unicode (UTF-8) and chmod gu+x, placed in the “Resources” folder of the script bundle.
#!/bin/bash
osascript <<END! - "$@"
on run {sourceFolder, targetFolder}
# set sourceFolder to text 2 thru -2 of sourceFolder
# set targetFolder to text 3 thru -2 of targetFolder
# maybe you'll need those two after all!
tell application "Finder"
set sourceFileNames to name of every item of folder sourceFolder
set targetFileNames to name of every item of folder targetFolder
repeat with aName in sourceFileNames
set aNameSource to alias (sourceFolder & aName)
set LabelIndexSource to label index of aNameSource
if aName is in targetFileNames then
set aNameTarget to alias ((targetFolder as string) & aName)
set label index of aNameTarget to LabelIndexSource
end if
end repeat
end tell
end run
END!
I’ve tried it both, with and without the two commented out lines. It doesn’t make any difference. Both fail with almost identical errors.
Here is the AS Set Job Labels.scptd script bundle. The two fatal errors in the shell script are included:
set sourceFolder to (path to documents folder as text) & "Job Templates:Label Templates:"
set targetDefLoc to (path to documents folder as text) & "Jobs:"
set targetFolder to choose folder default location alias targetDefLoc with prompt "Choose the TARGET Job folder to set the labels."
set flexiLabel to quoted form of POSIX path of ((path to me) & "Contents:Resources:flex20labeler" as string)
do shell script flexiLabel & " " & quoted form of POSIX path of sourceFolder & " " & quoted form of POSIX path of targetFolder with administrator privileges
”> this is the error with the two lines commented out in the shell script:
(*tell current application
path to current application
--> alias "HD-1:Users:chris:Desktop:Set Job Labels 2.7.scptd:"
do shell script "'/Users/chris/Desktop/Set Job Labels 2.7.scptd/Contents/Resources/flex20labeler' '/Users/chris/Documents/Job Templates/Label Templates/' '/Users/chris/Documents/Jobs/myTarget/'" with administrator privileges
--> error "244:248: execution error: Finder got an error: Can't get folder \"/Users/chris/Documents/Job Templates/Label Templates/\". (-1728)" number 1*)
”> and this is the error with the two lines enabled in the shell script:
(*tell current application
path to current application
--> alias "HD-1:Users:chris:Desktop:Set Job Labels 2.8.scptd:"
do shell script "'/Users/chris/Desktop/Set Job Labels 2.8.scptd/Contents/Resources/flex20labeler' '/Users/chris/Documents/Job Templates/Label Templates/' '/Users/chris/Documents/Jobs/myTarget/'" with administrator privileges
--> error "242:246: execution error: Finder got an error: Can't get folder \"Users/chris/Documents/Job Templates/Label Templates\". (-1728)" number 1*)
I’m still hoping that a solution is in sight. What’s the next step?
Cheers,
Chris
The automatic conversion of the parameters into posix paths, came rather unwarranted for.
I have just reworked the shell script to take height for this. and supplied two alternate lines if the script should fail due to the quoting.
#!/bin/bash
osascript <<END! - "$@"
on run {sourceFolder, targetFolder}
# maybe you'll need those two after all!
# set sourceFolder to text 2 thru -2 of ( POSIX file sourceFolder as alias as text)
# set targetFolder to text 3 thru -2 of (POSIX file targetFolder as alias as text)
set sourceFolder to POSIX file sourceFolder as alias as text
set targetFolder to POSIX file targetFolder as alias as text
tell application "Finder"
set sourceFileNames to name of every item of folder sourceFolder
set targetFileNames to name of every item of folder targetFolder
repeat with aName in sourceFileNames
set aNameSource to alias (sourceFolder & aName)
set LabelIndexSource to label index of aNameSource
if aName is in targetFileNames then
set aNameTarget to alias (targetFolder & aName)
set label index of aNameTarget to LabelIndexSource
end if
end repeat
end tell
end run
END!
Wow, finally, it did work
In the process, I’ve added something new to my bag of tricks
BTW, it works without the need for the commented out lines.
Many, many thanks for your help and patience
I am glad it finally did work. I will remember for a long time that hfspaths are automatically coerced into posix paths, at least in Apple Script version 2.1.2 and onwards, maybe it started earlier.
Anyways, I am glad I could help, and I have a head start for writing a script for setting label indexes from the command line, which I will post somewhere in codexchange at my leisure.