2015年07月

How to open Eclipse with attached console in OS X?

I need to launch my Eclipse RCP application with the terminal attached to it and cannot do this. Eclipse itself has the same problem. I have added the standard "-console" option to eclipse.ini but the console is not shown. However when I run Contents/MacOS/eclipse directly it works and shows the console! What is the difference in thse two ways of launching and why doesn't the simple way show the console?





Store all currently opened excel workbooks and open it later

I am currently facing a problem in which I want to:



1.Store all currently opened excel workbooks in a an array



2.Save and close the workbook



3.Open back all opened workbooks



4.Focus back to a specific workbook



The current code i have:



For Each wb In Application.Workbooks

wb.Save

Next wb


Works as expected but my different excel workbooks keeps 'flashing' which is kind of irritating, thus the need to save and close all.



I do understand that to focus back to a specific workbook u can use activate function. If i do an set array inside the 'For each loop', it will not work as it will become a double for loop.



As i'm new to VBA, i would really appreciate any input from you all.



Thank you!



Answers

I've given you two different options in this code. Either using a collection or an array.
You can step through a collection using For Each item in Collection loop while the array would need a For..Next loop.



Sub All_OpenWorkBooks_Collection()

Dim wrkBk As Workbook

''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Add to a collection '
''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim vItem As Variant
Dim colWorkBooks As Collection
Set colWorkBooks = New Collection

For Each wrkBk In Workbooks
If wrkBk.Name <> ThisWorkbook.Name Then
colWorkBooks.Add wrkBk.FullName
wrkBk.Close SaveChanges:=True
End If
Next wrkBk
Set wrkBk = Nothing

For Each vItem In colWorkBooks
Workbooks.Open (vItem)
Next vItem

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Set a reference to a specific workbook - can then use wrkBk to refer to it. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set wrkBk = Workbooks("Copy (4) of New Microsoft Excel Worksheet.xlsx")
wrkBk.Activate

End Sub

'------------------------------------------------------------------------

Sub All_OpenWorkbooks_Array()

Dim wrkBk As Workbook

''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Add to an array. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim x As Long
Dim arrWrkBk() As Variant
ReDim arrWrkBk(1 To Workbooks.Count)

For x = Workbooks.Count To 1 Step -1
If Workbooks(x).Name <> ThisWorkbook.Name Then
arrWrkBk(x) = Workbooks(x).FullName
Workbooks(x).Close SaveChanges:=True
End If
Next x

For x = 1 To UBound(arrWrkBk)
If arrWrkBk(x) <> "" Then
Workbooks.Open (arrWrkBk(x))
End If
Next x
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Set a reference to a specific workbook - can then use wrkBk to refer to it. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set wrkBk = Workbooks("Copy (4) of New Microsoft Excel Worksheet.xlsx")
wrkBk.Activate

End Sub


Edit: Note I step backwards through the array loop - as it's counting open workbooks and closing them the number of open workbooks goes down as the loop progresses (so when it got to loop number 4 there's a good chance that workbook number 4 has already been closed).



Edit 2: The comment on workspaces may be just what you're after - I'd check that out first.



Answers

Would adding
.ScreenUpdating = False
before your loop help?



And
.ScreenUpdating = true
after to switch it back on.





Running a Cordova project on WP8 in VS2015

I've just started a blank Cordova project on Visual Studio 2015, the intention being to use it to develop mobile apps in the future. I've been able to get it to run in the emulator on Android and even onto an iOS device (through a Mac), but I haven't been able to figure out how to run straight onto a WP8.1/Android device, or on a WP8 emulator. When I try, I get this message:




Error running one or more of the platforms: Error: cmd: Command failed with exit code 2



You may not have the required environment or OS to run this project




The Android device also says, above that:




ERROR: Failed to deploy to device, no devices found.




There's very little documentation online, and because it's all so new, Google hasn't been particularly enlightening. It's possible that I'm missing some software, but I don't know what.



For the devices, it could be that I'm supposed to select the device from a list somewhere, but I can't see anywhere that could be. For the emulator, I'm assuming I'm missing software, or a setting is wrong on my PC, but have no idea what it could be.



Answers

To fix the issue with WP8:



I had been playing around with different versions of VS... un-installing and re-installing 2013 and 2015 and eventually hit the same issue as you.



The solution here worked for me:



No Emulator lists to deploy windows phone app



I do not have an android phone to test with t the moment, so I have not seen the android device issue.





Scala “fluent” style programming

I'm learning Scala from the book "Scala for the Impatient". One section in the book illustrates the "fluent" style of programming with the following code snippet:



object Title

class Document {
private var useNextArgAs: Any = null
def set(obj: Title.type): this.type = { useNextArgAs = obj; this }
def to(arg: String) = if (useNextArgAs == Title) title = arg; else ...
}


The author then makes the following method call:



book set Title to "Scala for the Impatient"


Some questions here:




Why is useNextArgAs type Any? The only type that's used in the example for useNextArgAs is a Title. Can you show a use case for using Any instead of Title?
Method set returns this but the return type is this.type, not Document? Isn't this supposed to be the instance?
In method to, title = arg refers to non-existent var title? Where did it come from?

Answers

A possible (not entirely correct) implementation for this excercise from the book can be found (Spoiler Alert!) in github



The chapter 18 where this example comes from is labeled as "Senior Lib Designer" difficulty level. At this level a person should be well aware of re-use and extensibility issues, and consequences of mutable state in scala. This is definetely not a technic for beginners.



To OP's questions:



1) Why is useNextArgAs type Any? --- to make it support arbitrary attribute types in Document sub-classes. Document is not declared final



2) Method set returns this but the return type is this.type --- see Alvin Alexander's article below (you probably have the explanation in "Scala for the Impatient")




explicitly setting this.type as the return type of your set* methods
ensures that the fluent style will continue to work in your subclasses




consider the following extension and play around with set() and to() return type and value



class Book extends Document {
def bind :this.type = {/*do something*/; this}
}

val book = new Book
(book set Title to "Scala for the Impatient").bind


3) non-existent var title --- it is a mutable field in Document, just missing in the code snippet provided



Some extra reading on the subject:




http://alvinalexander.com/scala/how-to-fluent-style-programming-methods-in-scala
http://martinfowler.com/bliki/FluentInterface.html



How to Add/Remove Java desktop application to Windows Startup programmaticaly in Java?

I have a windows desktop application written on Java. I have a checkbox there saying "Launch at system startup". So if this checkbox is checked then i want the app to start when a user logs in to Windows. And if it's not checked then i want to remove it(If it already exists).



And i want to do it from my application using Java (I know there are some other methods like batch file and windows service).



I have checked Stack Overflow code but it didn't work. Actually i just want a solution like Code Project.



But unfortunately it's in C# .net. So how can i achieve this using Java ? I have searched a lot but couldn't find a workable solution.



Any help would be highly appreciated.



EDIT: I am also open for JNA/JNI approach. The thing is i just need to do it in Java. And it doesn't matter whatever i am using. I am ready to go for JNA/JNI.



Answers

Another option which is much simpeler than a COM solution is to add/remove a file to one of the Windows startup folders. You could do something like this:



    String allUsersStartupFolder = "C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Startup";
//String personalStartupFolder = "C:/Users/" + System.getProperty("user.name") + "/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup";

File startupFolder = new File(allUsersStartupFolder);
System.out.println(startupFolder);
File startupFile = new File(startupFolder, "MyProgramStartup.bat");

if(startupFile.exists()){
System.out.println("Unregister");
startupFile.delete();
}else{
System.out.println("Register");
Files.write(startupFile.toPath(), "java -jar MyProgram.jar".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}




↑このページのトップヘ