Groovy retrieve file version property (Windows) - windows

Could you please help me to retrieve file version property from Groovy script (in Windows platform)?
I mean the Version property available in Windows (7) in Details tab of file Properties window opened by right-click on file name.
I found to do it with WSH only.
Thanks In Advance!

First I tried to find a solution with and "More New I/O APIs for the Java™ Platform" (NIO.2) but didn't succeed. When I looked closer at your WSH-example I realized it is COM scripting.
So there are 2 possiblities to solve this:
Com4j
Java Native Access (JNA)
An example for accessing Word from Java can be found here.
Update
I tried to solve your problem, but run into an exception within the Namespace-function:
#Grab(group='net.java.dev.jna', module='platform', version='3.5.2')
import com.sun.jna.platform.win32.COM.COMException
import com.sun.jna.platform.win32.COM.COMObject
import com.sun.jna.platform.win32.OleAuto;
import com.sun.jna.platform.win32.Variant;
import com.sun.jna.platform.win32.Variant.VARIANT;
import com.sun.jna.platform.win32.WTypes.BSTR;
import com.sun.jna.platform.win32.WinNT.HRESULT;
public class Shell extends COMObject {
public Shell() throws COMException {
super("Shell.Application", false);
}
public HRESULT Namespace(String dir) throws COMException
{
def bstrDir = OleAuto.INSTANCE.SysAllocString(dir)
def varDir = new VARIANT(bstrDir)
def result = new VARIANT.ByReference()
HRESULT hr = oleMethod(OleAuto.DISPATCH_METHOD, result, this.iDispatch, "Namespace", varDir);
}
}
def shell = new Shell()
shell.Namespace("C:\\Temp")

Related

Groovy #CompileStatic and #TypeChecked order, bug or misunderstanding

I started getting a strange failure when compiling a gradle task class. This is the task I created:
package sample
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.gradle.api.artifacts.Dependency
import org.gradle.api.provider.Property
import org.gradle.api.tasks.AbstractCopyTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.bundling.Zip
import sample.internal.DataSourceXmlConfig
#TypeChecked
#CompileStatic
class DataSource extends Zip {
#Internal
final Property<File> warFile = project.objects.property(File.class)
DataSource() {
warFile.convention(project.provider {
def files = project.configurations.getByName('warApp').fileCollection { Dependency d ->
d.name == (archiveFileName.getOrElse("") - (~/\.[^.]+$/))
}
files.empty ? null : files.first()
})
}
/**
* This function is used to specify the location of data-sources.xml
* and injects it into the archive
* #param dsConf The configuration object used to specify the location of the
* file as well as any extra variables which should be injected into the file
*/
#Input
void dataSourceXml(#DelegatesTo(DataSourceXmlConfig) Closure dsConf) {
filesToUpdate {
DataSourceXmlConfig ds = new DataSourceXmlConfig()
dsConf.delegate = ds
dsConf.resolveStrategy = Closure.DELEGATE_FIRST
dsConf.call()
exclude('**/WEB-INF/classes/data-sources.xml')
from(ds.source) {
if (ds.expansions) {
expand(ds.expansions)
}
into('WEB-INF/classes/')
rename { 'data-sources.xml' }
}
}
}
private def filesToUpdate(#DelegatesTo(AbstractCopyTask) Closure action) {
action.delegate = this
action.resolveStrategy = Closure.DELEGATE_FIRST
if (warFile.isPresent()) {
from(project.zipTree(warFile)) {
action.call()
}
}
}
}
When groovy compiles this class, I get the following error:
Execution failed for task ':buildSrc:compileGroovy'.
BUG! exception in phase 'class generation' in source unit '/tmp/bus-server/buildSrc/src/main/groovy/sample/DataSource.groovy'
At line 28 column 28 On receiver: archiveFileName.getOrElse() with
message: minus and arguments: .[^.]+$ This method should not have
been called. Please try to create a simple example reproducing this
error and file a bug report at
https://issues.apache.org/jira/browse/GROOVY
Gradle version: 5.6
Groovy version: localGroovy() = 2.5.4
tl;dr, is this a bug or am I missing something about how these annotations work?
The first thing I tried to do was to remove either one of #TypeChecked and #CompileStatic annotations to see if the error goes away.
This actually fixed the problem right away. Compiling the source with either annotations added was successful, but fails when both are present.
I read some questions and answers regarding the use of both annotations, but none of them seemed to suggest that one cannot use both at the same time.
Finally, I tried switching the order of the annotations to see if that helps and to my surprise, it worked! No compilation errors!
This works:
#CompileStatic
#TypeChecked
class DataSource extends Zip { ... }
At this point, I guess my question would be, is this a bug or is there something I am not understanding about the use of both of these annotations? I'm leaning more towards it being a bug just because of the fact that the order made the error message go away.

There is no procedure with the name `example.convertDirection` registered for this database instance

I'm trying to create my own custom procedure using neo4J. I'm running the neo4j installer (v3.2.6) for windows.
I followed this tutorial and created my own simple procedure below:
package example;
import java.util.List;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;
public class ConvertDirection{
#UserFunction
#Description("example.convertDirection('v') - returns full direction name VERTICAL.")
public String convertDirection(
#Name("string") String string) {
if (string.equals(null)) {
return null;
}
if(string.equalsIgnoreCase("v")){
return "VERTICAL";
} else if (string.equalsIgnoreCase("h")){
return "HORIZONTAL";
} else {
return "BOTH";
}
}
}
I created my own test class and generated the jar running mvn clean package.
After that, I placed the procedure-template-1.0.0-SNAPSHOT in my both plugins folders (C:\Users\var\lib\neo4j\data\databases\graph.db\plugins and C:\Program Files\Neo4j CE 3.1.1\plugins).
Then I changed my neo4j.conf as suggested here to include the plugin paths. I've tried pointing both to program files/plugins and graph.db/plugins in both ways:
dbms.directories.plugins=c:/Users/var/lib/neo4j/data/databases/graph.db/plugins
dbms.directories.plugins=c:/Program\ Files/Neo4j\ CE\ 3.1.1/plugins
After restarting the server, I still got this error and my procedure isn't listed.
There is no procedure with the name example.convertDirection
registered for this database instance. Please ensure you've spelled
the procedure name correctly and that the procedure is properly
deployed.
Does anyone know what this might be?
You created a function, not a procedure (see #UserFunction in your code).
You should see it listed in call dbms.functions(), and as a function, CALL and YIELD are not needed, just use it inline like any other function.

AIR Native Process works on Mac, but not Windows

I'm building an AIR application with Flash CC, so that I can use Native Processes. Before I even got to integrating it into my full project, I created a small test project to see if it would work. I tried it in Windows and it didn't work. I switched over to Mac and got it to work there, though.
The Native Process is triggered by a key press. The file is published as a Windows installer, not an Air package. I have made sure to include extendedDesktop in the xml file. I have double checked the file paths. Are there any other reasons this wouldn't work?
package {
import flash.display.MovieClip;
import flash.filesystem.File;
import flash.events.KeyboardEvent;
import flash.desktop.NativeProcess;
import flash.desktop.NativeProcessStartupInfo;
public class Main extends MovieClip {
var exe:File = new File("C:\Windows\System32\notepad.exe");
var nativeProcess:NativeProcess = new NativeProcess();
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var args:Vector.<String> = new Vector.<String>();
public function startProcess(event:KeyboardEvent):void
{
if (event.keyCode == 65) {
nativeProcessStartupInfo.executable = exe;
args.push("C:/Users/Tristan/Documents/TVCR/airTestFile.rtf");
nativeProcessStartupInfo.arguments = args;
nativeProcess.start(nativeProcessStartupInfo);
}
}
}
}
How to output some debug information. The following code will show some messages if the keyboard events really trigger the handler method.
public function startProcess(event:KeyboardEvent):void
{
// LogText:TextField
LogText.appendText("\n\nKey Pressed: " + event.keyCode);
}
How to output and diagnose an exception:
try
{
nativeProcess.start(nativeProcessStartupInfo);
LogText.appendText("\n\nThe process has been started without exceptions.");
}
catch (fail:Error)
{
// LogText:TextField
LogText.appendText("\n\n" + fail.getStackTrace());
}
It seems that the file path I was using wasn't in the correct format.
C:\Windows\System32\notepad.exe
should be
C:\\Windows\\System32\\notepad.exe
The file path for Windows has to use a double backslash "\" since a single backslash gets interpreted as an escape.
Something that's handy, especially if you need to build a path to an executable or your making something that's going to be Windows and Mac is to use File.separator since it will do "\" for Windows and "/" for Mac/Linux
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html#separator

Compiling and Interpreting Packaged Software Assignment - Cant display multilple class outputs

This is a bit involved, and I want to explain this succinctly without making you read a lot, then show the code. I'm not good at that.
Barebones explanation. In this assignment, we are learning, how to compile,interpret in comand prompt, create a package, create a class and sub-classes, import the sub-classes, and execute println commands in all the sub-classes as a single compiled program, and display such in command prompt. I'm missing something, and the subclass println commands don't display when I run GreetingsClass.java, the Superclass. They are all in the same package. The package directory is com.cisp2030.course, and the three Chapters class files exist in a .Chapters folder inside of .course.
First, GreetingsClass.java:
package com.cisp2030.course;
import com.cisp2030.course.Chapters.*;
public class GreetingsClass
{
Chapter1 c1 = new Chapter1();
Chapter2 c2 = new Chapter2();
Chapter3 c3 = new Chapter3();
public static void main(String[] args)
{
System.out.println("$ Greetings, CISP2030!");
System.out.println(c1);
}
}
This is supposed to import and instantiate the variables of the next code contained as Chapter1.class in .Chapters folder.
package com.cisp2030.course.Chapters;
public class Chapter1
{
public Chapter1()
{
System.out.println("Hello from Chapter1!");
}
}
Just imagine that the above code is one of three that range from Chapter1-Chapter3, and they have been compiled by Command Prompt into class files in their respective directory.
The expected output of compiling, interpreting, and running this program in command prompt should be one program which displays all 3-4 println commands. However, at this time, running hte command prompt only displays the one println command. I think this is because I need to sub-class the Chapter classes to GreetingsClass, and having them imported already, somehow direct GreetingsClass to execute the commands of hte Chapters class files, but I don't know how, and I've googled this consistently, and searched through my textbook and am none the wiser. I think I'm missing something in the code itself, but I don't know enough to come up with any ideas. Any help or advice would be greatly appreciated.
Code has been finished:
package com.cisp2030.course;
import com.cisp2030.course.Chapters.*;
public class GreetingsClass
{
public static void main(String[] args)
{
System.out.println("$ Greetings, CISP2030!");
Chapter1 c1 = new Chapter1();
Chapter2 c2 = new Chapter2();
Chapter3 c3 = new Chapter3();
}
}
Does that even compile?
Java doesn't allow static constructors.
public Chapter1() // removed "static"
{
System.out.println("Hello from Chapter1!");
}
c1 is a member of GreetingsClass. In main() you are in a static method, not a method of GreetingsClass and so you can't access it's members. Most obvious solution is to add GreetingsClass greetings = new GreetingsClass(); to main and try to get c1 out of that.

Selenium RC User Defined Functions

Trying to do something simple -
I have a set of statements to clear browser cookies:
public void clearCookies () {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("10000");
selenium.deleteAllVisibleCookies();
}
Now, if I use this function in a test script (using TestNG), calls to this work perfectly. However, if I moved this function to a separate class and change the declaration to include "static", the "selenium" keyword is not recognized.
In a configuration class (say configClass),
public static void clearCookies () {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("30000");
selenium.deleteAllVisibleCookies();
}
Now, in my test script, if I call configClass.clearCookies();, I get a runtime error
I tried declaring DefaultSelenium selenium = new DefaultSelenium(null);, in the clearCookies() function, but that too results in a runtime error.
I do have the import com.thoughtworks.selenium.*; import in my configClass.
Any pointers would be appreciated. Thanks.
You can do two things.
Refer to the same selenium object in both the classes i.e. in configClass and the class you are calling configClass.clearCookies().
or else
send selenium object to the clearCookies. So the code would be like this
public static void clearCookies (DefaultSelenium selenium) {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("30000");
selenium.deleteAllVisibleCookies();
}

Resources