I have a VBScript that toggles proxy ON and OFF. I'd like to change the file icon accordingly, so that when the proxy is ON the file icon is a green tick and when it's OFF the icon is a red cross (meaning that I can see whether the proxy is active or not before running the script).
How do I change the icon programmatically? Just for THAT file, not ALL VBScripts!
You can't change the icon for a specific file. You can, however, change the icon of a specific shortcut to a file.
Set sh = CreateObject("WScript.Shell")
lnkfile = sh.SpecialFolders("Desktop") & "\your.lnk"
Set lnk = sh.CreateShortcut(lnkfile)
If lnk.IconLocation = "C:\path\to\some.ico" Then
lnk.IconLocation = "C:\path\to\.ico"
Else
lnk.IconLocation = "C:\path\to\some.ico"
End If
lnk.Save
If the shortcut is located in the All Users desktop folder (C:\Users\Public\Desktop) you need to replace "Desktop" with "AllUsersDesktop".
Related
I am using the following script to copy certain folder content from my hard-drive on a usb-Stick. Since the folder content might change, I am using aliases.
The script used to work perfectly, but since I had to make changes in my folder structure, I now have sometimes an alias of an alias (workaround of the script, won´t go into that).
Problem is that the script seems to only convert the first level of alias-folders, but if there is an alias for a file within an alias for a folder, it copies the file-alias.
I wonder if it is possible to tell the script to recursively go through every level of the folder (ie every file) and copy the original file instead.
Thanks!
Peter
ps: exemplary folder structure of source folder:
alias of folder 1
alias of folder 1-1
alias of file a, alias of file b
alias of folder 2
alias of file c
--> first a dialogue
display notification "hello, get folder ready"
delay 5 --> allow time for the notification to trigger
set theDialogText to "choose source"
display dialog theDialogText
--> Result: {button returned:"OK"}
--> now computing
set the backupSource to (choose folder) as alias
set theDialogText to "choose destination"
display dialog theDialogText
--> Result: {button returned:"OK"}
set the backupDestination to (choose folder) as alias
display notification "maybe this takes a while..."
delay 6 --> allow time for the notification to trigger
--> now copy
tell application "Finder"
activate
with timeout of 600 seconds --> equeals 10 Minutes
set everyAlias to every item of folder backupSource
repeat with thisAlias in everyAlias
set originalFile to (original item of thisAlias)
duplicate originalFile to backupDestination
end repeat
end timeout
end tell
``````````
I feel that the the line "repeat thisAlias in everyAlias" doesn´t do its job and goes only one level down, ie it converts the alias on the first level and not all other aliases within this alias-folder
I don't have a quick fix, but rather programming advice: it's time to add debugging. In this case, I suspect your script is not operating on the files you intend to use. So what's it doing instead?
For example, near the beginning,
set debugOn to true
or better
property debugOn : true
Then right before your duplicate command,
if debugOn then display dialog ¬
"Copying thisAlias: " & return & (thisAlias as text) & return & ¬
"of originalFile: " & return & originalFile buttons "OK" default button 1
…and so forth. You can leave these if debugOn then… checks throughout your code with dialogs or even different modes of behavior, and just change it to property debugOn : false once it's working. Having it all there is also great for the inevitable day you need to edit the script, but it's aged enough you've forgotten all its nuances.
Maybe this following AppleScript code will help you accomplish what you're looking for?
tell application "Finder"
set sourceFolder to choose folder with prompt "CHOOSE SOURCE"
set destinationFolder to choose folder with prompt "CHOOSE DESTINATION"
set nestedFolders to a reference to (entire contents of sourceFolder)
set aliasFiles to a reference to every alias file of nestedFolders
set aliasFiles to contents of aliasFiles
repeat with i from 1 to count of aliasFiles
set thisItem to item i of aliasFiles
if not (exists of original item of thisItem) then
delete thisItem
end if
end repeat
set aliasFiles to a reference to every alias file of nestedFolders
set originalFiles to (original item of aliasFiles) as alias list
duplicate originalFiles to destinationFolder
end tell
I actually have two questions.
How to exclude hidden files like .DS_STORE, Icon when I try to get files in folder ?
I've tried "without invisibles" but it seems not working.
How to set my var the_new_folder as an existing folder if already exists ?
Thanks for answers.
My code:
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
tell application "Finder"
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
end tell
on file_to_folder(the_folder)
tell application "Finder"
-- HELP NEEDED HERE
-- HOW TO EXCLUDE HIDDEN FILES (Like Icon, .DS_STORE, etc)
set the_files to files of the_folder
repeat with the_file in the_files
-- Exclude folder in selection
if kind of the_file is not "Folder" then
set the_path to container of the_file
set the_file_ext to name extension of the_file
-- Remove extension of the file name
set the_file_name to name of the_file as string
set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name
-- Make the new folder with the file name
try
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
on error
-- HELP NEEDED HERE
-- HOW TO SET the_new_folder AS THE EXISTING FOLDER
end try
-- Move the file in the new folder
move the_file to the_new_folder
end if
end repeat
end tell
end file_to_folder
tell application "Finder"
(display dialog ("It's done!") buttons {"Perfect!"})
end tell
Using the System Events context instead of Finder:
bypasses the problem with the AppleShowAllFiles preference[1]
is much faster in general.
Using the visible property of file / folder objects in the System Events context allows you to predictably determine either all items, including hidden ones (by default), or only the visible ones (with whose visible is true):
# Sample input path.
set the_path to POSIX path of (path to home folder)
tell application "System Events"
set allVisibleFiles to files of folder the_path whose visible is true
end tell
Simply omit whose visible is true to include hidden files too.
The code for either referencing a preexisting folder or creating it on demand is essentially the same as in the Finder context:
# Sample input path.
set the_path to POSIX path of (path to home folder)
# Sample subfolder name
set the_subfolder_name to "subfolder"
tell application "System Events"
if folder (the_path & the_subfolder_name) exists then
set subfolder to folder (the_path & the_subfolder_name)
else
set subfolder to make new folder at folder the_path ¬
with properties {name: the_subfolder_name}
end if
end tell
[1] In order to predictably exclude hidden items, a Finder-based solution is not only cumbersome but has massive side effects:
You need to determine the current state of the the AppleShowAllFiles preference (defaults read com.apple.Finder AppleShowAllFiles),
then turn it off.
then kill Finder to make the change take effect (it restarts automatically) - this will be visually disruptive
then, after your code has run, restore it to its previous value.
then kill Finder again so that the restored value takes effect again.
I believe that your first question is not a problem. Your code works fine for me.
As for the second issue, the simplest method is to use the text representation of the_path, and simply build the new folder, and see if it already exists:
set the_path_Text to (the_path as text)
set try_Path to the_path_Text & the_file_name
if (folder try_Path) exists then
set the_new_folder to (folder try_Path)
else
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
end if
If you are truly having difficulty with the first code section, please post a new question with more details, such as a copy of the Result section of the Script.
Thank you to all of you ! #mklement0 #craig-smith
You will find below the corrected code with your help!
If I share this code, you want to be cited for thanks?
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
-- Get user folder
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
on file_to_folder(the_folder)
tell application "System Events"
-- Get all files without hidden
set the_files to files of the_folder whose visible is true
repeat with the_file in the_files
-- Remove extension of the file name
set the_file_ext to name extension of the_file
set the_file_name to name of the_file as string
set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name
-- Make a new folder or get the existing
set the_path to POSIX path of the_folder
if folder (the_path & the_file_name) exists then
set the_new_folder to folder (the_path & the_file_name)
else
set the_new_folder to make new folder at folder the_path with properties {name:the_file_name}
end if
-- Move the file to the new folder
move the_file to the_new_folder
end repeat
end tell
end file_to_folder
-- Ending dialog
display dialog ("It's done!") buttons {"Perfect!"}
This seems like such an easy thing to do that I'm amazed the solution is still eluding me.
I'm trying to store that last folder opened by a "choose file" dialog box. I want to store that folder's location in a text file.
I want to have my "choose file" dialog always open to the last folder used.
I've got a lot of the script working but there is one weird thing that keeps eluding me.
Look at my script...
set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")
set strPathFromConfig to item 1 of Shows as string
set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)
display dialog strPathFromConfig
set strPath to (path to home folder as text) & strPathFromConfig
display dialog strPath
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath
The script reads my config text file which contains one line and only one string of "Documents".
I trim some leading garbage characters and display a dialog with the result of "Documents".
Then I set the strPath using the the value from the config file.
I display the new value is it is a valid location on my system.
Next I attempt to the dialog and I get an error message of "File alias Macintosh HD:Users:lowken:Documents of wasn't found.
Let change the script so that instead of using the value that was extracted from the config.txt file, I simply set a string variable in my script.
set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")
set strPathFromConfig to item 1 of Shows as string
set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)
display dialog strPathFromConfig
set strTemp to "Documents"
set strPath to (path to home folder as text) & strTemp
display dialog strPath
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath
Now it works. AppleScript seems to not want to use the value that was looked up from the config.txt file.
What am I doing wrong? I tried casting to an Alias I tried different locations on my system.
It seems to me that somehow the value looked up from the text file is not a string data type.
Any ideas?
P.S.
I'm running OS X Yosemite 10.10.4 on a mid 2012 MacBook Pro.
The problem is the encoding of your "config.txt" document.
The read command can read (MacRoman, UTF-8 or UTF-16) encoded text files, but you must specify the type class, otherwise it read the text file as MacRoman.
set strPathFromConfig to paragraph 1 of (read "/Users/lowken/Documents/config.txt" as «class ut16») -- or if UTF-8 use —> as «class utf8»
set strPath to ((path to home folder as text) & strPathFromConfig) as alias
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location strPath
For another encoding, you must change the encoding of the "config.txt" document.
The default location of the choose file command can be a Unix path "/Users/my_user/Documents" or a Finder path with Alias before like : alias "HD:Users:my_User:Documents"
so first check that strPath is the correct value, then if OK, double check the class of it :
Display Dialog (class of strPath) as string
it may not be OK and you have to coerce the (path to home folder as string) and not text.
I've been searching google.com a couple of hours trying to find a way to do this, but there is no answer. Everywhere is resize to the longest size, but nothing about resize to the specified width only.
Is there any way in Automator or AppleScript to resize images to a specified width only, instead of just the longest size? I need my output images to be a specific width ony (e.g 200px).
You can do this with plain AppleScript and a shell utility entitled sips:
on open droppings
repeat with everyDrop in droppings
set originalFile to quoted form of POSIX path of (everyDrop as text)
tell application "Finder"
set originalName to everyDrop's name
set imageContainer to (everyDrop's container as text)
end tell
set reSizedName to "200W" & originalName
set outputPath to quoted form of POSIX path of (imageContainer & reSizedName)
do shell script "sips --resampleWidth 200 " & originalFile & " --out " & outputPath
end repeat
end open
on run
display dialog "Drop some Image Files to Re-size them all to 200 pixels wide" buttons {"Aye Aye"} default button "Aye Aye"
end run
This preserves the aspect ratio of the original image, and simply re-sizes the width to 200 pixels. Hopefully you can see where you can make the changes necessary for your own workflow.
If you want to drop a folder of images as well as individual files, try this as a droplet:
on open droppings
repeat with everyDrop in droppings
if (info for everyDrop)'s folder is true then
tell application "Finder" to set allImageFiles to everyDrop's every file
repeat with eachFile in allImageFiles
my SetWidthTo200(eachFile)
end repeat
else
my SetWidthTo200(everyDrop)
end if
end repeat
end open
to SetWidthTo200(img)
set originalFile to quoted form of POSIX path of (img as text)
tell application "Finder"
set originalName to img's name
set imageContainer to (img's container as text)
end tell
set reSizedName to "200W" & originalName
set outputPath to quoted form of POSIX path of (imageContainer & reSizedName)
do shell script "sips --resampleWidth 200 " & originalFile & " --out " & outputPath
end SetWidthTo200
on run
display dialog "Drop some Image Files to Re-size them all to 200 pixels wide" buttons {"Aye Aye"} default button "Aye Aye"
end run
There is still no error-checking or checking to be sure that the files are indeed image files, so keep that in mind.
Here's a summary from this OSX Tips using automator to resize images
open "Automator"
File > New > Service
Settings:
Service received selected: "image files"
in "Finder"
in the left pane, find for action named "Get Selected Finder Items"
Next look for action "Scale Images"
If you wish to replace original, choose "Don’t Add" when it ask
"would you like to add a Copy Finder Items action", otherwise, choose add
In order to be able to choose the highest pixels before scaling, click on Options at the right of results, tick
"show this action when the workflow run"
How I can create a shortcut from Computer(real shortcut with Manage option in right click) on desktop from CMD?
Here's an example:
Create a blank text file somewhere. In my test, I used the root of C:\
Open the text file, and paste in the following vbscript code:
Const MY_COMPUTER = &H11&
Dim Act, Desktop, Link, ObjF, ObjFItem, Shell
Set Act = CreateObject("Wscript.Shell")
Set Shell = CreateObject("Shell.Application")
Set ObjF = Shell.Namespace(MY_COMPUTER)
Desktop = Act.SpecialFolders("Desktop")
Set Link = Act.CreateShortcut(Desktop & "\Computer.lnk")
Set ObjFItem = ObjF.Self
Link.TargetPath = ObjFItem.Path
Link.WindowStyle = 1
Link.IconLocation = "Shell32.dll, 15"
Link.Description = "Shortcut To My Computer"
Link.WorkingDirectory = Desktop
Link.Save
Save the file with a .vbs extension.
Run from the command line, for example:
C:\Documents and Settings\bridge> c:\test.vbs (presuming you called the file test.vbs).
You can customise the shortcut by editing the file above, for example the description or name of the shortcut created.