How do I command Automator to convert the multiple images from PNG to ICNS once at all? - png

I use OS X Mountain 10.8.2.
I used Automator and added AppleScript, copying the command from the old app's developer at the Iconoodle - Convenient ICNS to PNG image conversion (Iconoodle isn't supported by OS X 10.8 by being a PPC app). I saved the workflow as an application. I selected the images and dropped them into the application, but the images weren't converted to ICNS. I also copied from the question Convert image to .icns file AppleScript Obj-C and it didn't work either.
I'm really frustrated with it. I want to use Automator to convert the multiple images from PNG to ICNS once at all. It's very annoying to convert each image to ICNS in the icon editor applications, one for one. The Preview.app doesn't want to convert the image in format PNG to ICNS because it has only one page, it can convert it to ICNS only if the icon has many pages.
Do you know to make it?
Thank you for your attention, help and patience!

Here is another approach...
NOTE: Your ORIGINAL pngs will be scaled to the expected size before the icns file is created. If you want to keep a copy of the original png, duplicate it first. You can also add a line or so to the script to do so automatically.
property expectedSizes : {16, 32, 48, 128, 256, 512, 1024, 9999999}
set myFiles to choose file with multiple selections allowed
repeat with aFile in myFiles
tell application "System Events" to set bPath to POSIX path of (container of aFile)
set newPath to bPath & "/" & bName(aFile) & ".icns" as text
set aFile to quoted form of (POSIX path of aFile)
set {W, H} to {paragraph 1, paragraph 2} of (do shell script "sips -g pixelWidth -g pixelHeight " & aFile & " | grep -Eo [0-9]*$")
set {W, H} to {W as number, H as number}
if W > H then
set W to eSize(W)
do shell script "sips " & aFile & " -Z " & W & " -p " & W & space & W & " --padColor FFFFFF -i"
delay 1
else if H > W then
set H to eSize(H)
do shell script "sips " & aFile & " -Z " & H & " -p " & H & space & H & " --padColor FFFFFF -i"
delay 1
-- H = W but not in expected sizes
else if H is not in expectedSizes then
set H to eSize(H)
do shell script "sips " & aFile & " -Z " & H & " -p " & H & space & H & " --padColor FFFFFF -i"
delay 1
end if
do shell script "sips -s format icns " & aFile & " --out " & quoted form of newPath
end repeat
on bName(theFile)
tell application "Finder" to set {name:fileName, name extension:nameExtension} to theFile
set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
end bName
on eSize(lDimen)
repeat with i from 1 to 8
if lDimen < item i of expectedSizes then return item (i - 1) of expectedSizes
end repeat
end eSize

You can still run the iconoodle script by just pasting it to AppleScript Editor. It's just a wrapper for sips, but it can only convert icns to png.
You can use sips directly to convert png to icns:
for f in *.png; do sips -s format icns "$f" --out "${f%png}icns"; done

Related

AutoHotKey - How to take a screenshot and to paste it to a *.jpg file?

How to take a screenshot in Windows 8 and to paste it to a *.jpg file using AutoHotKey script? I want my custom key combination and folder for images
{CustomKey}:: ;image saved in Pictures/Screenshots folder by default
Send #{PrintScreen}
Return
The images are saved as png; can then be converted to jpg and moved to another directory.
Tested in Win 10.
Might work in Win 8 too.
Another question similar asked here:
https://autohotkey.com/board/topic/63742-how-to-save-a-screen-shot-with-ahk/
Problem was that saved file was blank. In my case, the file turned out to be all grey.
Reason was that FileAppend saved as text file.
To save screenshot image taken with PrintScrn button:
; Send {PrintScreen}
FileAppend %ClipboardAll%, FileName.raw, UTF-8
; This can be read back to memory with:
FileRead, Clipboard, *c FileName.raw
; image can also be converted/compressed to save space.
; I already had ffmpeg, so put this:
Run %ComSpec% /c "ffmpeg.exe -f rawvideo -pixel_format rgb32 -s 2256x1504 -i FileName.raw -vf hflip -vf vflip output.png"
Tested on Win 10.
You might try using the Gdip_All.ahk library. I found a maintained version of it at: https://github.com/mmikeww/AHKv2-Gdip/blob/master/Gdip_All.ahk
If you have Irfan View installed you can use this:
run('"C:\Program Files\IrfanView\i_view64.exe" /capture=0 "/convert=path_to_file.png" /jpgq=95')
You can also specify format or quality and include process name, time or other staff, for example:
dir := 'D:\screenshots'
name := winGetProcessName('A') ' ' a_YYYY '-' a_MM '-' a_DD ' ' a_hour '-' a_min '-' a_sec
format := 'png'
quality_jpeg := 95
run('"C:\Program Files\IrfanView\i_view64.exe" /capture=0 "/convert=' dir '\' name '.' format '" /jpgq=' quality_jpeg)
You can check Irfan View command line options in the program help or here.
You can also search some other programs that can silently make screenshots using command line options. If you find another good one let me know.

GhostScript, How to set custom page size using command line?

I know that have a simillar question : How to change Ghostscript output file (in printer spooler)
but the answers was not helpful
I using the command :
-q -dBATCH -dNOPAUSE -dNODISPLAY -c "mark /NoCancel true /OutputFile (%printer%Ricoh c2051) /UserSettings << /DocumentName (Test) /MaxResolution 600 >> (mswinpr2) finddevice putdeviceprops setdevice" -f "C:\duplex.pdf"
to set a custom name for document, but if a try to use "-sPAPERSIZE=a4" still the outputpaper size is "LETTER".
then,how to change the paper type when I use these settings?
----Edit
after researching, I found out how to set the paper type.
just added "/ PageSize [595 842]" (a4) at the prompt
then the line becomes:
-q-dBATCH-dNOPAUSE-dNODISPLAY-c "mark / NoCancel true / OutputFile (% printer% Ricoh c2051) / PageSize [595 842] / UserSettings << / DocumentName (Test) / maxResolution 600 >> (mswinpr2) FindDevice setdevice putdeviceprops "-f" C: \ duplex.pdf "****
Your basic problem is that unless you set a fixed media size Ghostscript will set the media size to that which is requested n the PDF file (it will do this for PostScript input as well). Set -dFIXEDMEDIA to fix the media size.
In addition, the mswinpr2 device does not attempt to set the media size on the printer (assuming it has more than one), but simply uses the default.

VBS to find file with date and to change that date to the previous day

New here and VERY new to VBS so please easy.
I have a set of files that are sent via FTP to a folder on a server, the naming structure of these files are:
waw_29_2013-09-09_act_v001.csv
waw_29_2013-09-09_amf_v001.csv
waw_29_2013-09-09_inc_v001.csv
waw_29_2013-09-09_nbs_v001.csv
waw_29_2013-09-09_trn_v001.csv
waw_29_2013-09-09_val_v001.csv
waw_29_2013-09-09_wth_v001.csv
What I am trying to achieve is for a script to scan the folder that contains these files, and change the name to the previous day but keep the structure of the naming convention.
I have tried this on a file in my c:\ drive (I am very new so please excuse my lack of understanding)
**Dim file1, file2
file1 = "C:\fake_%date%.txt"
file2 = "C:\waw_" & year(date) & -month(date) & -Day(Date) & "_act_v001.csv"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file1) = true then
If fso.FileExists(file2) = true then
fso.DeleteFile(file2)
End if
fso.MoveFile file1, file2
'else
' msgbox "File does not exist"
End If
Set FSO = Nothing**
The is on a file (as a test) called c:\fake_2013-09-09.txt and it is trying to scan the c:\ drive for that file (regardless of the date) and then rename this file to "waw_29_2013-09-09_act_v001.csv"
.
I also need the same script to run separately but to change the date back 3 days for weekends too.
Any help would be hugely appreciated.
Take care
Mike
Hello again
I have gotten a little further:
I have now amended the script to look as follows:
Dim file1, file2
file1 = "C:\winter\waw_29_2013-09-10_act_v001.csv"
file2 = "C:\winter\waw_29_" & -Year(Date) & Right("0" & Month(Date),2) & Right
("0" & -Day(Date),2) & "_act_v001.csv"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file1) = true then
If fso.FileExists(file2) = true then
fso.DeleteFile(file2)
End if
fso.MoveFile file1, file2
'else
' msgbox "File does not exist"
End If
Set FSO = Nothing
Now I have 2 questions about the script as I think it is 90% there:
The line in File1, how can I get this to search for the file but to only search on the and _act field?
Under File2, I can get the system to now produce a date for the file (the wrong date but I am getting there) but the format is 20130909 and not 2013-09-09 as I need. How do I add in the "-" between dates as it fails if I leave the "& Right("0")" in to create the leading "0" but if I take that out, it will put the "-" in but no leading )!!!
My last question is I have found the DateAdd command that allows me to search the file and then change the date to the previous date.....my problem is I don't know where to put that in the code.
Guys if you can help, I would be really grateful.
Mike
WOW thank you so much, really helped a lot and solved the second part of my issue.
Do you know how to search a folder for a set parameter so I can search a folder named "waw_29_2013-09-09_act_v001.csv" but to exclude the date section from the search?
Again thank you so much, I learnt a lot from that part of the script
Mike
For building a date string of the form YYYY-MM-DD you need to concatenate the date portions and the interleaving hyphens:
d = Date
Year(d) & "-" & Right("0" & Month(d), 2) & "-" & Right("0" & Day(d), 2)
For building a date string of 3 days ago you do the same, but subtract 3 days from the current date first:
d = Date - 3
Year(d) & "-" & Right("0" & Month(d), 2) & "-" & Right("0" & Day(d), 2)

substitution within a text file, using Applescript and sed

The question is a sequel to plain text URL to HTML code (Automator/AppleScript).
Suppose I have a plain txt file /Users/myname/Desktop/URLlist.txt:
title 1
http://a.b/c
title 2
http://d.e/f
...
I'd like to (1) convert all the URL (http://...) to HTML code, and (2) add
<br />
to each empty line, so that the aforementioned content will become:
title 1
http://a.b/c
<br />
title 2
http://d.e/f
<br />
...
I come to the following Applescript:
set inFile to "/Users/myname/Desktop/URLlist.txt"
set middleFile to "/Users/myname/Desktop/URLlist2.txt"
set outFile to "/Users/myname/Desktop/URLlist3.txt"
do shell script "sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' " & quoted form of inFile & " >" & quoted form of middleFile
do shell script "sed 's/^$/\\ <br \\/>/g' " & quoted form of middleFile & " >" & quoted form of outFile
It works, but it is redundant (and silly?). Could anyone make it more succinct? Can it be done involving only one text file instead of three (i.e. the original content in /Users/myname/Desktop/URLlist.txt is overwritten with the end result)?
Thank you very much in advance.
Try:
set inFile to "/Users/myname/Desktop/URLlist.txt"
set myData to (do shell script "sed '
/\\(http[^ ]*\\)/ a\\
<br />
' " & quoted form of inFile & " | sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' ")
do shell script "echo " & quoted form of myData & " > " & quoted form of inFile
This will let you use the myData variable later in your script. If this is not part of a larger script and you are simply modifying your file, use the -i option as jackjr300 suggests. Also, this script looks for the original pattern and appends the new line to it rather than simply looking for empty lines.
EDIT:
set inFile to "/Users/myname/Desktop/URLlist.txt"
set myData to (do shell script "sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g; s/^$/\\ <br \\/>/g' " & quoted form of inFile)
do shell script "echo " & quoted form of myData & " > " & quoted form of inFile
Use the -i '' option to edit files in-place.
set inFile to "/Users/myname/Desktop/URLlist.txt"
do shell script "sed -i '' 's:^$:\\ <br />:; s:\\(http[^ ]*\\):\\1:g' " & quoted form of inFile
If you want a copy of the original file, use a specified extension like sed -i ' copy'
--
Updated:
A `DOCTYPE is a required preamble.
DOCTYPEs are required for legacy reasons. When omitted, browsers tend to use a different rendering mode that is incompatible with some specifications. Including the DOCTYPE in a document ensures that the browser makes a best-effort attempt at following the relevant specifications.
The HTML lang attribute can be used to declare the language of a Web page or a portion of a Web page. This is meant to assist search engines and browsers. According to the W3C recommendation you should declare the primary language for each Web page with the lang attribute inside the <html> tag
The <meta> tag provides metadata about the HTML document. <meta> tags always goes inside the <head> element.
The http-equiv attribute provides an HTTP header for the information/value of the content attribute.
content: the value associated with the http-equiv or name attribute.
charset: To display an HTML page correctly, the browser must know what character-set to use.
In this script: I put "utf-8" as encoding, change it by the encoding of your original file.
set inFile to "/Users/myname/Desktop/URLlist.html" -- text file with a ".html" extension
set nL to linefeed
set prepandHTML to "<!DOCTYPE html>\\" & nL & "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\" lang=\"en-US\">\\" & nL & tab & "<head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\\" & nL & "</head>\\" & nL
do shell script "sed -i '' 's:^$:\\ <br />:; s:\\(http[^ ]*\\):\\1:g; 1s~^~" & prepandHTML & "~' " & quoted form of inFile
do shell script "echo '</html>' " & quoted form of inFile -- write last HTML tag
I can't understand sed commands very well (it makes my brain hurt) so here's the applescript way to do this task. Hope it helps.
set f to (path to desktop as text) & "URLlist.txt"
set emptyLine to " <br />"
set htmlLine1 to "<a href=\""
set htmlLine2 to "\">"
set htmlLine3 to "</a>"
-- read the file into a list
set fileList to paragraphs of (read file f)
-- modify the file as required into a new list
set newList to {}
repeat with i from 1 to count of fileList
set thisItem to item i of fileList
if thisItem is "" then
set end of newList to emptyLine
else if thisItem starts with "http" then
set end of newList to htmlLine1 & thisItem & htmlLine2 & thisItem & htmlLine3
else
set end of newList to thisItem
end if
end repeat
-- make the new list into a string
set text item delimiters to return
set newFile to newList as text
set text item delimiters to ""
-- write the new string back to the file overwriting its contents
set openFile to open for access file f with write permission
write newFile to openFile starting at 0 as text
close access openFile
EDIT: if you have trouble with the encoding these 2 handlers will handle the read/write properly. So just insert them in the code and adjust those lines to use the handlers. Good luck.
NOTE: when opening the file using TextEdit, use the File menu and open specifically as UTF-8.
on writeTo_UTF8(targetFile, theText, appendText)
try
set targetFile to targetFile as text
set openFile to open for access file targetFile with write permission
if appendText is false then
set eof of openFile to 0
write «data rdatEFBBBF» to openFile starting at eof -- UTF-8 BOM
else
tell application "Finder" to set fileExists to exists file targetFile
if fileExists is false then
set eof of openFile to 0
write «data rdatEFBBBF» to openFile starting at eof -- UTF-8 BOM
end if
end if
write theText as «class utf8» to openFile starting at eof
close access openFile
return true
on error theError
try
close access file targetFile
end try
return theError
end try
end writeTo_UTF8
on readFrom_UTF8(targetFile)
try
set targetFile to targetFile as text
targetFile as alias -- if file doesn't exist then you get an error
set openFile to open for access file targetFile
set theText to read openFile as «class utf8»
close access openFile
return theText
on error
try
close access file targetFile
end try
return false
end try
end readFrom_UTF8

Unicode named Folder shows ? in wscript prompt

I am facing problems with Unicode named folders. When I drag the folder to the script, it doesn't show the path of the folder properly.
Simple VBScript (this is just a portion of it):
Dim Wshso : Set Wshso = WScript.CreateObject("WScript.Shell")
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count = 1 Then
If FSO.FileExists(Wscript.Arguments.Item(0)) = true and FSO.FolderExists(Wscript.Arguments.Item(0)) = false Then
Alert "You dragged a file, not a folder! My god." & vbcrlf & "Script will terminate immediately", 0, "Alert: User is stupid", 48
WScript.Quit
Else
targetDir = WScript.Arguments.Item(0)
Wshso.Popup targetDir
End If
Else
targetDir = Wshso.SpecialFolders("Desktop")
Alert "Note: No folder to traverse detected, default set to:" & vbcrlf & Wshso.SpecialFolders("Desktop"), 0, "Alert", 48
End If
If it is a normal path without Unicode characters, it's fine. But in this case:
Directory: 4minute (포미닛) - Hit Your Heart
Then it will show something like 4minute (?) - Hit Your Heart
And if I do a FolderExists it can't find the dragged folder.
Is there any workaround to support Unicode named Folders?
Thanks!
I'll edit if this is not clear enough
This does seem to be a problem peculiar to the Windows Script Host's DropHandler shell extension. Whereas:
test.vbs "C:\포미닛.txt"
C:\WINDOWS\System32\WScript.exe "test.vbs" "C:\포미닛.txt"
both work when typed from the console (even if the console can't render the Hangul so it looks like ?), a drag and drop operation that should result in the same command goes through a Unicode->ANSI->Unicode translation that loses all characters that aren't in the current ANSI code page. (So 포미닛 will work on a default Korean Windows install but not Western.)
I'm not aware of a proper way to fix the problem. You could perhaps work around it by changing the DropHandler for .vbs files in the registry:
HKEY_CLASSES_ROOT\VBSFile\ShellEx\DropHandler\(Default)
from the WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) to {86C86720-42A0-1069-A2E8-08002B30309D}, the one used for .exe, .bat and similar, which doesn't suffer from this issue. You would also probably have to change the file association for .vbs to put quotes around the filename argument too, since the EXE DropHandler doesn't, to avoid problems with spaces in filenames.
Since this affects argument-passing for all VBS files it would be a perilous fix to deploy on any machine but your own. If you needed to do that, maybe you could try creating a new file extension with the appropriate DropTarget rather than changing VBSFile itself? Or maybe forgo drop-onto-script behaviour and provide a file Open dialog or manual drop field instead.
For anyone landing here from Google...
Bobince's tip lead me to work around this problem by wrapping my vbscript file (myscript.vbs) in a dos batch file (mybatch.bat).
The tip was:
"Seem to be a problem peculiar to the Windows Script Host's
DropHandler shell extension whereas.... the one used for .exe, .bat and
similar... doesn't suffer from this issue."
mybatch.bat contains:
:Loop
IF "%1"=="" GOTO Continue
set allfiles=%allfiles% "%1"
SHIFT
GOTO Loop
:Continue
"myscript.vbs" %allfiles%
You may also find this code from my myscript.vbs to be helpful
For Each strFullFileName In Wscript.Arguments
' do stuff
Next
Based on DG's answer, if you just want to accept one file as drop target then you can write a batch file (if you have it named as "x.bat" place VBScript with filename "x.bat.vbs" at same folder) that just contains:
#"%0.vbs" %1
the # means to not output the row on the display (I found it to show garbage text even if you use chcp 1250 as first command)
don't use double-quotes around %1, it won't work if your VBScript uses logic like the following (code I was using below was from http://jeffkinzer.blogspot.com/2012/06/vbscript-to-convert-excel-to-csv.html). Tested it and it works fine with spaces in the file and folder names:
Dim strExcelFileName
strExcelFileName = WScript.Arguments.Item(0) 'file name to parse
' get path where script is running
strScript = WScript.ScriptFullName
Dim fso
Set fso = CreateObject ("Scripting.FileSystemObject")
strScriptPath = fso.GetAbsolutePathName(strScript & "\..")
Set fso = Nothing
' If the Input file is NOT qualified with a path, default the current path
LPosition = InStrRev(strExcelFileName, "\")
if LPosition = 0 Then 'no folder path
strExcelFileName = strScriptPath & "\" & strExcelFileName
strScriptPath = strScriptPath & "\"
else 'there is a folder path, use it for the output folder path also
strScriptPath = Mid(strExcelFileName, 1, LPosition)
End If
' msgbox LPosition & " - " & strExcelFileName & " - " & strScriptPath
Modify WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) to {86C86720-42A0-1069-A2E8-08002B30309D} and add this function to convert short path to long:
Function Short2Long(shortFullPath)
dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(shortFullPath)
Set app = CreateObject("Shell.Application")
Short2Long = app.NameSpace(f.ParentFolder.Path).ParseName(f.Name).Path
end function