ghostscript命令行中的可变数据 [英] variable data in ghostscript commandline

查看:812
本文介绍了ghostscript命令行中的可变数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK ...这是我再次。相同的项目。我试图循环通过目录中的文件,并设置所有PDF上的页面大小。我有困难获得文件名的参数,我需要通过他们通过ghostscript。我放弃了更改.run shell命令中的参数,并决定在变量中创建参数(如您所见)。我的问题似乎在我传递的strParam变量。它看起来正确当我在msgbox中检查它,但是当脚本运行我没有得到任何文件创建。如果我硬编码的2个参数我需要改变(-sOUTPUTFILE =和文件名)它的工作原理。

  Dim FSO,FLD,FIL,X 
Dim strInput,strFolder,strParam

strInput = InputBox(1 - Landscape& chr(13)&2 - Portrait图像方向)
strFolder = InputBox(复制并粘贴文件夹位置& chr(13)&到您的扫描,文件位置)
设置objShell = WScript.CreateObject WScript.Shell)

如果strInput = 1,则
调用LandScape
ElseIf strInput = 2然后
调用Portrait
Else
MsgBox 您的输入无效,单击确定退出
结束如果


Sub LandScape
ObjShell.CurrentDirectory = strFolder
Wscript.Echo objShell.CurrentDirectory
MsgBox您的图像是风景
MsgBox您的文件位于:& strFolder
设置FSO = CreateObject(Scripting.FileSystemObject)
设置FLD = FSO.GetFolder(strFolder)

对于FLD.Files中的每个FIL
strParam = chr(34)& -dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS = 2592 -dDEVICEHEIGHTPOINTS = 1728 -dFIXEDMEDIA -sDEVICE = pdfwrite -sOutputFile = new-& FIL.Name& & FIL.Name& chr(34)& chr(34)& chr(34)
MsgBox strParam
objShell.Runc:\Program Files\gs\gs9.04\bin\gswin64c.exe& strParam
Next

End Sub


解决方案

由于你的问题是由.Run方法的参数引起的,你应该构建一个strCmd变量并显示它(而不是只有strParam),以排除最后的连接弄乱了事情。



如果你这样做,你可以复制&粘贴命令并从命令行进行测试,而不是使用不同命令进行检查(例如,硬编码命令中没有-dDEVICEWIDTHPOINTS = 2592)。



由于我无法测试你的命令,我所能做的只是指出

结尾处的3个引号

 c:\Program Files\gs\gs9.04\bin\gswin64c.exe-dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS = 2592 -dDEVICEHEIGHT 
POINTS = 1728 - dFIXEDMEDIA -sDEVICE = pdfwrite -sOutputFile = new-04.vbs 04.vbs



您应该以结构化的方式构建您的(复杂)命令行,背景参见此处这里(如果你不容易混淆)。



更新wrt @ Bond的回答: p>

毫无疑问,Bond的以结构化方式构建命令行的方式是正确的。它将为许多人和许多类似的任务工作很好。但是人们的接线方式不同,所以它可能付出知道另一种方式来皮肤这些猫:

 选项显式
功能qq(s):qq =& s& :End Function
Dim sFiNa:sFiNa =justafile.ext
Dim sCmd:sCmd = Join(Array(_
qq(c:\Program Files \ gs\gs9.04\bin\gswin64c.exe)_
,-dQUIET -dNOPAUSE dBATCH_
,-dDEVICEWIDTHPOINTS = 2592 -dDEVICEHEIGHTPOINTS = 1728_
,-dFIXEDMEDIA -sDEVICE = pdfwrite_
,-sOutputFile =& qq(sFiNa)_
,qq(sFiNa)_
))
WScript.Echo sCmd

输出:

 c:\Program Files\gs\gs9.04\bin\gswin64c.exe-dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS = 2592 -dDEVICEHEIGHTPOINTS = 1728 -dFIXEDMEDIA -sDEVICE = pdfwrite  - sOutputFile =justafile.extjustafile.ext




  1. 实施一次;无需检查6次出现的
    Chr(34)以防止一个拼写错误 Chr(43)

  2. Join自动提供参数之间的空格;没有
    需要检查每个&

  3. 因为完整的命令行是构建,所以可以测试;相反,
    的正确性 strExe& & strParms (是真的有一个空格
    之间的引号或一些有趣的字母,显示像一个空格
    在我的编辑器;?我错误的strParams(而不是使用Option $

(4)测试可能会抛出一个错误,例如input和输出必须不同。如果你没有像

中那样有额外的噪音(和通过复制和粘贴程序的诱惑),这样的问题更容易被捕获。

  strParams = strParams& Chr(34)& FIL.Name& Chr(34)
strParams = strParams&
! - noise -----------------------! !--------!
strParams = strParams& Chr(34)& FIL.Name& Chr(34)

vs:

 !----! ! 
,-sOutputFile =& qq(sFiNa)_

此外,参数的含义不会隐藏在列123ff一个语句3行之前,它不能通过添加-dNoErrorsPlease参数以后搞乱。可能的修复

 ,-sOutputFile =& qq(new-& sFiNa)_ 

看起来很复杂,但对于VBScripter :输出到添加了前缀'new-'的文件,引用以避免文件名中的空格出现问题。


OK... It's me again. Same project. I'm trying to loop through files in a directory and set the page size on all the PDFs. I'm having difficulty getting the file names in the parameters I need to pass them through ghostscript. I gave up on changing the parameters IN the .run shell command and decided to create the parameters in a variable (as you can see). My problem seems to be in the strParam variable that I am passing. It looks correct when I check it in the msgbox but when the script runs I don't get any files created. If I hardcode the 2 parameters I need to change ("-sOUTPUTFILE=" and file name) it works as expected.

Dim FSO, FLD, FIL, X
Dim strInput, strFolder, strParam

strInput = InputBox ("1 - Landscape" & chr(13) & "2 - Portrait", "Original Image   Orientation")
strFolder = InputBox ("Copy and paste the folder location" & chr(13) & "to your scans",  "File Location")
Set objShell = WScript.CreateObject("WScript.Shell")

If strInput=1 then  
    Call LandScape 
ElseIf strInput=2 Then
    Call Portrait
Else
    MsgBox "Your entry is invalid.  Click OK to exit"
End If


Sub LandScape
ObjShell.CurrentDirectory= strFolder
Wscript.Echo objShell.CurrentDirectory
    MsgBox "Your images are Landscape"
    MsgBox "Your files are located at: " & strFolder
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FLD = FSO.GetFolder(strFolder)

    For Each FIL In FLD.Files
        strParam = chr(34) & " -dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=new-" & FIL.Name & " " & FIL.Name & chr(34) & chr(34) & chr(34)
        MsgBox strParam
        objShell.Run """c:\Program Files\gs\gs9.04\bin\gswin64c.exe" & strParam
    Next

End Sub

解决方案

As your problem is caused by the parameter to the .Run method, you should build a strCmd variable and display that (instead of just strParam) to rule out that the final concatenation messes things up.

If you do that, you can copy & paste the command and test it from a command line instead of doing a check with a different command (e.g. no -dDEVICEWIDTHPOINTS=2592 in your hard-coded command).

As I can't test you command(s), all I can do is point out that the 3 quotes at the end of

"c:\Program Files\gs\gs9.04\bin\gswin64c.exe" -dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHT
POINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=new-04.vbs 04.vbs"""

look fishy. Do you want to quote both file names?

You should build your (complex) command line in a structured way. For background see here and here (if you aren't confused easily).

Update wrt @Bond's answer:

There can't be any doubt that Bond's way of 'building a command line in a structured way' is sound. It will work for many people and many comparable tasks just fine. But people are wired differently, so it may pay to know another way to skin those cats:

Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim sFiNa : sFiNa = "justafile.ext"
Dim sCmd  : sCmd  = Join(Array( _
     qq("c:\Program Files\gs\gs9.04\bin\gswin64c.exe") _
   , "-dQUIET -dNOPAUSE dBATCH" _
   , "-dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728" _
   , "-dFIXEDMEDIA -sDEVICE=pdfwrite" _
   , "-sOutputFile=" & qq(sFiNa) _
   , qq(sFiNa) _
))
WScript.Echo sCmd

output:

"c:\Program Files\gs\gs9.04\bin\gswin64c.exe" -dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile="justafile.ext" "justafile.ext"

  1. The quoting is implemented once; no need to check 6 occurrences of Chr(34) to guard against one typo Chr(43).
  2. The Join provides the spaces between arguments automagically; no need to check each & for "Need I a space here too?".
  3. Because the full command line is build, it can be tested; in contrast, the correctness of strExe & " " & strParms (is there really a space between the quotes or some funny letter that displays like a space in my editor?; did I misspell strParams (while not using "Option Explicit")?) may still be in doubt.

(4) The test may well throw an error like "input and output must be different". Such a problem is easier to catch if you don't have extra noise (and the temptation to program by copy & paste) as in

strParams = strParams & Chr(34) & FIL.Name & Chr(34)
strParams = strParams & " "
!-- noise -----------------------!        !--------!
strParams = strParams & Chr(34) & FIL.Name & Chr(34)

vs:

                 !----!     !
, "-sOutputFile=" & qq(sFiNa) _

In addition, the meaning of the parameter isn't hidden in columns 123ff in a statement 3 lines before, and it can't messed up by adding the "-dNoErrorsPlease" argument later. A possible fix

, "-sOutputFile=" & qq("new-" & sFiNa) _

looks complicated but (for a VBScripter) obviously means: "output to file with added prefix 'new-', quoted to avoid problems with spaces in file names".

这篇关于ghostscript命令行中的可变数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆